What's wrong with this code?

for( let i = 1 ; j < 13 ; i++ ) {
    for( let i = 1 ; j < 13 ; j++) {
        console.log(`${j} multiplied by ${i} is ${i*j}`);
    }
}

I’m going through a book trying to learn some Javascript. This is the code I enter but it won’t work, just get a lot of error messages, Would love some help!!

Thanks, Tom

You must be consistent with the use of the variable here. You used i, then j, then i. Use i for all three statements in the condition.

Same for [quote=“trctvb, post:1, topic:287647”]
for(let i=1 ; j<13 ; j++){
[/quote]

Because this for loop is nested inside the first one, you should not use the variable i again. make all three statements in this condition use j.

So the for loop would be like this:

for (let i=1; i<13; i++) {
   for (let j=1; j < 13; j++) {
      // code goes here
   }
}
3 Likes

Hi, and thanks. I’ve worked with loops before though it’s been a while. This came from a book so I just assumed it was right. So the loop is working but the output isn’t quite right. Instead of numbers I"m seeing

"{j} multiplied by {i} is {i*j}.

console.log(${j} multiplied by ${i} is ${i*j});

Thanks, Tom

@trctvb

Start developing the habit of never using j as a variable. When you need a single letter variable, stick to using k, n, etc. Basically, letters that don’t look similar and can’t be confused with one another.

When developers use j mixed in their loops, this is actually a pretty common mistake so it’s best to just not use it. Since i stands for iterator, so it stays.

I’m trying to learn JavaScript so am not an expert, but I can’t see anything in the documentation for console.log that suggests the syntax you are using would work. There’s no mention of using a dollar-sign to include variable values directly in the code. https://developer.mozilla.org/en-US/docs/Web/API/console#Outputting_text_to_the_console

It looks more like a PHP construct, which would only work using double-quotes to surround the string.

There’s no mention of using a dollar-sign to include variable values directly in the code.

@droopsnoot

He’s using Template Literals. Those are backticks around it, not single quotes.

I :heart: Template Literals

3 Likes

Me too. They’re a very useful tool.

Ah, thanks. I had a look to see if backticks might be causing an issue simply by not being quotes, but didn’t pursue past the end of the console documentation.

Could it be a browser problem? What browser are you using @trctvb ?

Backticks are required for Template Literals.

But you’re right, browser version could be the problem.

To help reduce confusion, an updated version of the code is at https://jsfiddle.net/8kdshqrc/1/

for (let i = 1; i <= 12; i += 1) {
  for (let j = 1; j <= 12; j += 1) {
    console.log(`${j} multiplied by ${i} is ${i*j}`);
  }
}

Thank you all for the help!!! Got it working and learned some things too. Thanks so much!!

Tom

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.