Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Cheryl Oliver
Cheryl Oliver
21,676 Points

Multiplication in Javascript

Can someone help me with this please? Using your favourite programming language, write a short bit of code to output all the times tables from 1 to 12. i.e.: 1 x 1 = 1; 1 x 2 = 2; etc, through to 12 x 1 = 12; 12 x 2 = 24; etc.

1 Answer

Steven Parker
Steven Parker
231,248 Points

What course asks you to use "your favourite programming language"?

But anyway... :point_right: This sounds like a job easily managed with two nested loops and an output statement.

Here's how it might look in Python:

for multiplicand in range(1, 13):
    for multiplier  in range(1, 13):
        print("{} x {} = {}".format(multiplicand, multiplier, multiplicand * multiplier))
    print()

I wish this was a challenge I could accept. It sounds fun but I am not fluent in any programming languages yet. Although I would be interested to see someone's java code, just to see how they would do it.

Cheryl Oliver
Cheryl Oliver
21,676 Points

Hi Steven, Thanks but I dont know how to do a nested loop, can you show me?

Steven Parker
Steven Parker
231,248 Points

What course is this for? Can you give a link to a video or challenge?

"Nested" just means "one inside the other". And I added a sample to my answer.

Oops, I forgot this was the JavaScript forum (it was that "favorite language" thing). Here's the same example in JavaScript:

for (var multiplicand=1; multiplicand<13; multiplicand++)
    for (var multiplier=1; multiplier<13; multiplier++)
        console.log(multiplicand + " x " + multiplier + " = " + multiplicand * multiplier)
    console.log()