Diference between let and var

why this code doesnt work with variable declaration “let” ?

  function multiplyAll(arr) {
 var product=1;
  // Only change code below this line

 for (var i = 0; i < arr.length; i++){
  for (var j = 0; j < arr[i].length; j++){
    product *= arr[i][j];
  }
}
  // Only change code above this line
  return product;
}
multiplyAll([[5, 1], [0.2, 4, 0.5], [3, 9]]);

//IF I USE LET I DOESNT WORK...

/*function multiplyAll(arr) {
  let product = 1;
  // Only change code below this line
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; j++) {
      product = product * arr[i][j];
    }
  }
  // Only change code above this line
  return product;
}

// Modify values below to test your code
multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);
*/

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

What does “it doesn’t work” mean here?

First of all, always include a link to the challenge.

Second of all, if I use your “let” code, it works for me.

Could you please cut and paste the entire contents of the code pane that is not working but you think it should.

2 Likes

It means that if you use “let” instead of “var” the exercise dont pass the chlalenge

… Which tests fail? What additional information can you provide?

1 Like

Again, I cut and paste your “let” code into the challenge, and it passes for me. The commented out “let” code in your post works. So, either you are making some other mistake or there is something else going on.

Again, cut and paste the exact and complete code that you think should pass but doesn’t.

2 Likes

this is the challenge:

if you only change the word “var” for the word “let” does not pass the challenge

This just isn’t true. let works just fine. There is some other problem with your code or browser. So, again, can you please

a) provide more information about which test cases specifically are not passing for you

b) provide absolutely all of the code in your editor when you attempt to pass with ‘let’

If you refuse to provide more information, we can’t troublestoot your problem.

The Ask For Help button would have provided basic information about your browser that could be useful in debugging what is wrong on your end.

1 Like

Now it works fine. I don’t know what was happening, but when I first did the challenge the only way to pass it was changing the type of the variables to “var”.

thanks all for your time!.

1 Like

Glad its working now.

Often this can mean there was a subtle typo you didn’t notice, which is why we wanted to see the actual code that was not working instead of commented out code or a description of how to change your code into something you think won’t work.

2 Likes

Thanks, the code that wasnt working was this:

function multiplyAll(arr) {
  let product = 1;
  // Only change code below this line
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; j++) {
      product = product * arr[i][j];
    }
  }
  // Only change code above this line
  return product;
}

// Modify values below to test your code
multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);

This was the code I wrote and was not working well. But suddenly now is working, I don’t know why… I didnt change anything.

I known that there was no “subtle typo” error, because I only modify the type of the variables (products, i & j) from let to var to make it work.

do you understand?

Now its working.

I would stop using whatever browser you are using then. The two most probable explanations that come to mind are that you have a problem with your browser and it doesn’t properly and consistently run JavaScript or you had a subtle error that you did not notice. If its impossible that you made a tiny mistake, then your browser is probably broken.

1 Like

Yes, what browser are you using on what kind of device? Is the browser up to date?

The other possibility is that the browser works fine and just got into a “weird state”. Sometimes, it things that should work aren’t, it’s good to reboot and/or clear the browser cache.

1 Like

I copied and pasted your answer with let and it passed for me. Could you have accidentally added something that isn’t pasted above?