1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Loop for, condition 2, doesn't comply

Discussion in 'Programming/Internet' started by Sindre Eiklid, Sep 12, 2018.

  1. I'm a student with limited JavaScript knowledge (first year) and need help with Project Euler #2.


    var arr = [1, 2];
    var total = 2;
    var x = 0;
    var y = 1;
    for (var i; total < 4000000; x++, y++) {
    i = arr[x] + arr[y];
    arr.push(i);
    if (i % 2 == 0) {
    total += i
    }
    }
    console.log(total);

    My loop is supposed to stop after total is equal to 4,000,000 but for some reason, the total is 4,613,732.

    Login To add answer/comment
     

Share This Page