# If ==/=== keeps flagging false

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));
``````
``````  **Your browser information:**
``````

User Agent is: `Lastest Chrome`.

Challenge: Sum All Odd Fibonacci Numbers

Could you explain with your words what should be happening in the function?

1 Like
``````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 is what gets crazy. Even if I hard code fibSum to equal 2 and then change the if fibSum equals 2, it still flags false.

What do you expect to happen? `fibSum` to be always `2`?

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 .

As it can be seen, realizing some things regarding calculating Fibonacci sequence is also part of that challenge.

If you start at `i = 0`, `fibSum = 0` and `fibSeq = [0,0]`, then `fibSum === i` will only be true once.

Let’s walk though the steps:

``````i = 0;
fibSum = 0;
fibSeq = [0,0];

if (0 === 0) { // true
fibSeq[1] = 0; // fibSeq is now [0, 0]
fibSum = 0 + 0; // 0
fibSeq[0] = 0; // fibSeq is now [0, 0]
}
if (0 === 1) { // false
}
...
``````
1 Like