How can I change my reduce() to add until I get the number which was asked for?

Your code so far

function sumFibs(num) {
let numArr=[];
for(let i=0;i<=num;i++){
numArr.push(i)
}
console.log()
let oddNumArr=numArr.filter(numbers=>numbers % 2 !== 0)
oddNumArr.unshift(1)
let reducedArr=oddNumArr.reduce((a, b) => a + b, 0)
return reducedArr;
console.log(reducedArr)
}
sumFibs(4);

Your browser information:

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

The first thing to look at is your numArr. You want to find the odd numbers in the array, which you accomplish with oddNumArr successfully. But your initial numArr is not a Fibonacci sequence.

console.log(numArr) shows that numArr is EVERY number from 0 to num, not every Fibonacci number.

how May I do it? I want to start in 0. Then, I want to sum it with the nexs one: 1. Then, I want to get 1 again, because of 0+1, then and I want to get the sum of 1+1= 2 and I want to start that cycle again, but with those humbers.

You don’t need to slice the array. Remember your dot and bracket notation? If I have an array [1, 1, 2], then console.log(array[0]) would show 1.

What are you telling your loop to do with those conditions? If you run sumFib(10), your loop would generate [1, 1, 2, 3, 5, 8, 13, 21, 34, 55], the first 10 numbers in the Fibonacci sequence. You don’t want this part of your function to do that. You want this part of your function to generate the numbers less than 10.

Now we can move on to your for loop. Like I mentioned before, your let i=0;i<=num;i++ conditions will not work correctly. But I think we need to look at what actions are performed in the loop first, to help you understand what the conditions should be.

for(let i=0;i<=num;i++){
//so what do we need to put here??
}

We want this loop to generate the next number in the Fibonacci sequence each time it runs, right? If the next number in the sequence is the sum of the two previous numbers, how would you write the code to find that?

Remember that our Fibonacci sequence starts with 1,1, which we set up in our numArr. So build on that.

That’s a good start! numArr.push(Y) will do exactly what you want it to do. That would get you numArr = [1, 1, 2].

But using numArr[0] and numArr[1] in your loop would continue to push 2. You’d end up with an array [1, 1, 2, 2, 2, 2, 2, 2]. How would you change numArr[0] and numArr[1] to look at the last two numbers in the array each time the loop runs. Remember: the array will grow each time the loop runs because you push the Y value onto the array.

Now that you set the actions in your loop, we need to look at the condition.
Assume again that I am running sumFib(10). let i=0;i=num;i++, as you have written, will tell the loop to run 11 times (because i starts at 0). We don’t want the loop to run 10 times. We want the loop to run until the last number in our array is greater than num (the value submitted to the function - in my example, 10). How would you do that?

It is almost done! This is the last challenge I need to pass sumFibs(4000000) should return 4613732.

function sumFibs(num) {
let numArr = [1, 1];
for(let i=0;i<num;i++){
let y=numArr[0+i] +numArr[1+i]
numArr.push(y)
}
console.log(numArr)
let oddNumArr=numArr.filter(numbers=>numbers % 2 !== 0)
let lessThanNumArr=oddNumArr.filter(n=>n<=num)
console.log(oddNumArr)
let reducedArr=lessThanNumArr.reduce((a, b) => a + b, 0)
console.log(reducedArr)
return reducedArr;
}
sumFibs(4);

Not quite. Let’s break down your condition here. let i=0 This basically serves as a counter. You’re using this to track the values in your array with the numArr[0+i] code. i++ This tells the code to add 1 to the value of i at the end of each iteration of the loop.

i<num This tells the loop to continue running as long as i is smaller than the number being run through the function. In our example, it is 10. This is what we need to change.