*Intermediate Algorithm Scripting: Sum All Odd Fibonacci Numbers

Tell us what’s happening:

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.

Challenge: Sum All Odd Fibonacci Numbers

Link to the challenge:

Hello!

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.

1 Like

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.

Let’s start with just your numArr loop. Think about how arrays work, and try to complete these following steps:

  1. Start with numArr = [1, 1]. That’s the beginning of a Fibonacci sequence.
  2. The next number in a Fibonacci sequence is the sum of the previous two numbers. How would you retrieve those two numbers from the array?
  3. You need to continue finding the Fibonacci sequence numbers until you reach the num value in the function. How would you write that loop?
1 Like
  1. What do you mean with retrieve? I can use slice to copy the array.
  2. I can use a cycle for like this to make a loop until I get that number.
for(let i=0;i<=num;i++){
 

 
   }  

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.

1 Like

Therefore, I need to change the for cycle, but how may I set the numbers in the array starting with [1,1]

for(let i=0;i=num;i++){
 

 
   }

You don’t need to set it in the loop.

function sumFibs(num) {
let numArr=[];

You can set it here at the beginning of your function.

???

function sumFibs(num,i) {
let numArr=[];

Now you’ve changed what variables I would need to provide to run the function. You don’t want to do that.

How can you make numArr equal `[1, 1] at the beginning of the function?

1 Like

I changed it to its last form and I set let numArr=[1, 1]

function sumFibs(num) {
let numArr=[1, 1];

Great job!

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 is the part that I do not know :frowning:

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

Okay, let me see if I can help!

You have your numArr, which starts as [1, 1].
There are two steps we need to take here.

  1. We need to get a sum of the last two numbers in the array.
    • How do you tell your code to read a specific value in an array?
    • Once you read those two values, how do you add them together and store the total in a variable?
  2. We need to insert that sum at the end of the array.
    • How do you add an item to an array? There are four commands you should have reviewed for this so far.

1-I can use numArr[0] and numArr[1] to access to both values and I can set a new variable for example let Y= numArr[0] +numArr[1]

2 I can set numArr.push( Y)

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.

let  numArr = [1, 1,].
for(let i=0;i=num;i++){
let y=numArr[0+i] +numArr[1+i]
numArr.push(y) 
}

Nice work! Just get rid of that . on your numArr. :slight_smile:

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?

1 Like

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.