Am I confusing iterations and elements in an array?

Hello,

New to coding and first post so please go easy on me if I’m not posting correctly.

This code is from the recursion challenge in basic JavaScript, but my question is not about recursion (at least at the moment but I’m sure that’ll change, and I changed the code to add instead of multiply). I think I’m getting confused when it comes to distinguishing between the iteration of a loop and an element in an array. I keep thinking the array should start at [0] and so total *= arr[0] should equal 1 + 2 as 2 is my first element in my array. This isn’t what happens however. The console returns 1. Is this because at iteration [0] when n equals 0, i is not less than n and so the code won’t execute the iteration. Does this make arr[0] equal to 0 or null? Not sure how to think about this properly.

Please post your actual code instead of a screenshot of the code. Thanks

Hello!
I created your function on the browser console and the result for the same function input that you provide on your image is:
image
When n = 0, the argument arr is irrelevant because the loop should execute only when i < n and this occurs when n > 0 (because i is initialized on for loop with 0 by default). So when n = 0 the result is 1 whatever arr is:
image

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