DEV Community

k@k
k@k

Posted on

JavaScript Challenge #1

let array = [0,1,1,2,3,5,8];

...........
...........
[1,1,2,3,5,8]
[2,2,3,5,8]
[4,3,5,8]
[7,5,8]
[12,8]
[20]

Under the problem solve...make your solution comments..

Discussion (1)

Collapse
bairrada97 profile image
bairrada97 • Edited on

const challenge = array => {
  let elements = array;
  let newArray = elements;

  for (let i = 0; i < array.length + 1; i++) {
    let [first, second] = newArray;
    let sumTwoFirstElements = first + second;

    newArray.splice(0, 2);
    newArray = [sumTwoFirstElements, ...newArray];
    console.log(newArray);
  }
};
challenge([0, 1, 1, 2, 3, 5, 8]);