Sum All Odd Fibonacci Numbers unshift rather than push

Sum All Odd Fibonacci Numbers unshift rather than push
0

#1

Tell us what’s happening:
So, when I look at the intermediate solution, it uses arr.unshift, rather than arr.push. When I tested arr.push, it returns an infinite loop. Why is this, how is the order affected, when the starting array uses the same numbers?

P.S. the code is a work in progress, I want to try and come up with my own code with better understanding.

Your code so far


function sumFibs(num) {
if (num < 0) return -1;
if(num === 0) return 0;
if(num === 1) return 1;


var Fibs = [1, 1];
let nextFib = 0;

while((nextFib = Fibs[0] + Fibs[1]) <= num){ 
  Fibs.unshift(nextFib);
}

return Fibs.reduce((acc, curr) => {
        return acc + curr * (curr % 2);
    });


   
}

// test here
sumFibs(100);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers


#2

Array.unshift() is placing the new element at the beginning of the array each time, in effect creating a new Fibs[0] and moving what WAS Fibs[0] to Fibs[1]. If you are pushing the elements onto an array, and you use the LAST to to create the new el, what does your Array.push() code look like?