There is this solution that reminds me of a question I had before. This is a function that creates an array of numbers that are Fibonacci numbers to a certain term supplied by the argument. I have tested substituting something with i - 1.
fibonacci.push(fibonacci[i] + fibonacci[i - 1])
This creates an error. It seems that i - 1 is inaccessible. I haven’t found answers to this. How does this not work?
“carrimaxx, post:1, topic:408743”
function sumFibs(num) {
let fibonacci = [1,1]
for(var i = 0; i <= num; i++){
fibonacci.push(fibonacci[i] + fibonacci[fibonacci.length - 1]);
}
return fibonacci;
}
console.log(sumFibs(4));
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.63
.
Challenge: Sum All Odd Fibonacci Numbers
Link to the challenge: