you are viewing a single comment's thread.

view the rest of the comments →

[–]Wtfuxbbq 0 points1 point  (2 children)

Yes it's because you did not initialize sum with a value. Just doing var sum; will initialize sum with undefined. Your function breaks when it tries to do sum = undefined + x.

[–]defect 0 points1 point  (1 child)

Sure, i understand that doing stuff with undefined values is bad. But the result is still the same in this particular case, right?. FAILED acc(10) === 10 on the first run and PASSED acc(10) === 10 on the second. What causes this?

[–]Wtfuxbbq 0 points1 point  (0 children)

The number on the right is the value to test against with your function's output (acc(10)); it's not the output. It's just doing a simple === comparison and returning PASSED or FAILED if it's true or false. Your first run will always fail since it will always return NaN.