I watch the video course "programming lessons" from loftblog, the theme is cycles. Faced a problem - I completely rewrite the code itself, as in the video. Everything works for them, for me, no. Here is the code:

var arr = [1,2,3,4,5]; for(i=0; sum=0; i<arr.length; i++) { sum+=arr[i]; }; console.log(sum); 

this is what the console issues:

 "SyntaxError: Unexpected token ; at https://static.jsbin.com/js/prod/runner-4.1.4.min.js:1:13924 at https://static.jsbin.com/js/prod/runner-4.1.4.min.js:1:10866" 

in the comments to the video a few more people write that this code does not work for them. What is the problem?

Closed due to the fact that off-topic participants andreymal , 0xdb , Grundy , ߊߚߤߘ , dlarchikov Aug 20 '18 at 8:29 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - andreymal, Grundy, ߊߚߤߘ, dlarchikov
If the question can be reformulated according to the rules set out in the certificate , edit it .

    2 answers 2

    Replace the semicolon with a comma:

     for(i=0, sum=0; i<arr.length; i++) 
    • thank! I really didn't notice, although I looked at the code several times - Guest

    First, in the loop, forgot var in the declaration of the variable.

    Secondly, the parameters need to be separated by a comma.

    Thirdly, this is a recommendation, incorrect work with the scope in the code. sum must be declared before the loop.

     var arr = [1, 2, 3, 4, 5]; var sum = 0; for (var i = 0; i < arr.length; i++) { sum += arr[i]; } console.log(sum); 
    • Thanks, I really didn’t carefully rewrite the code, but declaring variables wasn’t necessary, although I tried it too, but the problem was precisely in commas - Guest