After i is equal to 1, everything starts flagging false, and for the love of me I cannot figure out why. Even with the “===” the if statement continues to flag false.
**Your code so far**
function sumFibs(num) {
let fibSeq = [0, 0];
let fibSum = 0;
for(let i = 0; i < num; i++) {
if(fibSum === i) {
fibSeq[1] = i;
fibSum = fibSeq[0] + fibSeq[1];
fibSeq[0] = fibSeq[1];
}
}
return fibSum;
}
console.log(sumFibs(4));
function sumFibs(num) {
let fibSeq = [0, 0];
let fibSum = 0;
for(let i = 0; i < num; i++) {
//first loop: i === 0, fibSum === 0
//second loop: i === 1, fibSum === 0
//this if is failing because it does increment on the first loop
//as i is 0, but it can't enter on the second loop because it
//did not increment on the first so fibSum is still 0 when
//i is 1
if(fibSum === i) {
//0 = 0
fibSeq[1] = i;
//0 = 0 + 0
fibSum = fibSeq[0] + fibSeq[1];
//0 = 0
fibSeq[0] = fibSeq[1];
}
}
return fibSum;
}
console.log(sumFibs(4));
this let fibSeq = [0, 0] should be this let fibSeq = [1, 1]
this let fibSum = 0 should be this let fibSum = 2
If the sequence started on [0,0] it would never increment.
In your loop you should change the values of the sequence regardless of a condition, and that would be best done in a while loop rather than a for loop. The only condition needed in the the loop is when to add to fibSum, and that condition is the whole point of the challenge .