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.
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.
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”.
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.
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.
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.
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.