Fibonacci sequecne help

Hi guys, I need help with these lines:

function sumFibs(num) {
let x = 1;//pushing in fibonaci sequence
let fn = [1]; // fibonaci sequence
let i = 1; // loop count
while (i <= num) {
fn.push(x);
x++
i++;
}
console.log(fn)
for (let i = 0; i < fn.length; i++) {
  console.log(fn[0] + fn[i] === fn[i])
}

From the array I’ve got I am trying to filter out the numbers that are not fibonacci but I can’t seem to be applying my logic here. What I am trying to do is whenever two numbers don’t have the summ of the 3rd number I am going to slice that specific number.
I am not sure how to do it if someone can hint me a bit would be great, ty :slight_smile:

  **Your code so far**

function sumFibs(num) {
let x = 1;//pushing in fibonaci sequence
let fn = [1]; // fibonaci sequence
let i = 1; // loop count
while (i <= num) {
fn.push(x);
x++
i++;
}
console.log(fn)
for (let i = 0; i < fn.length; i++) {
  console.log(fn[0] + fn[i] === fn[i])
}
/*const filtered = fn.filter(numb => {
  return numb % 2 !== 0
})
console.log(filtered)*/

/*for (let i = 0; i < filtered.length; i++) {
  console.log(filtered[i] + filtered[i])
  if (filtered[i] + filtered[i] >= num) {
    filtered.pop();
  }
  console.log(filtered)
}*/

/*const summ = filtered.reduce((previousValue, currentValue) => {
  return previousValue + currentValue;
})
console.log(filtered)
console.log (summ)*/
}

sumFibs(10);
  **Your browser information:**

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

Challenge: Sum All Odd Fibonacci Numbers

Link to the challenge:

Explain to me in plain language what the purpose of this code is.

I defined a number that equals to 1
an array [1]
I defined another number that equals to 1
while the number I is smaller or equal to num(parameter), I am pushing the number x to the array
x increases
i increases

Yes, I understand what it is doing. I wanted to know why you are doing this. What is the purpose of doing this? Your comment

// fibonaci sequence

leads me to believe you intend to create an array of the Fibonacci sequence. Is this true? Are you actually doing that?

Well I need to make the sequence that was my idea of it. so I made an array and pushed numbers smaller or equal to the parameter.

I was thinking how to do it but I couldn’t manage, but I figured I can probably remove the numbers I don’t need but I got stuck.

It doesn’t look like you are getting Fibonacci numbers to begin with.

To make Fibonacci numbers you just take the previous two numbers and add them together. So if we start with let say an array of [1,1] you would add those together, which would equal 2. So to keep making Fibonacci numbers lets keep that 2 so [1,1,2]. Now we need to sum the last two numbers again which are 1 and 2, which would be 3. So now we have [1,1,2,3]. lets just keep doing that over and over so 2 + 3 = 5. now we have [1,1,2,3,5] and so on till we hit whatever a number <= num. That should give you good Fibonacci numbers to work with and you can work on the other parts of the algorithm in terms of getting the odd numbers.

I think i used an array when i did this test just because i was more comfortable doing it that way. But you can probably come up with a way to do it without one that will probably perform much better then mine did.

1 Like

Yeah, you definitely don’t need an array, just a small number of variables.

its been over a week , i just came back to this.
I still seem to be stuck I am using an array because rest of my solution including an array, and this is the only issue I have.
I can’t seem to add TWO numbers of the array.

function sumFibs(num) {
  let arr = [1]; // fibonaci sequence
  let x = 1;
  for (let i = 0; i < arr.length; i++) {
    while (x <= num) {
    arr.push(x);
    x++;
    }
  }

This is what I have, I know it is not correct but I just can’t seem to figure out how to add those two numbers.

What is the formula for making Fbonacci numbers? You should start with two numbers and add one at a time based upon the value of the last two.

Thanks , i’ll google it and try it tomorrow its late on my time zone now.

hi,

  let arr = [1, 1]; // fibonaci sequence
  let x = arr[0] + arr[1];
  while (x <= num) {
    //arr.push(x) 
  }

I tried googling didn’t get much help from it, anyway.
This is basically my idea. My issue is once I push X I obvioulsy have more numbers in array, how do I make that next number add with the previous one, I can’t seem to find a way.

How do you access the last two elements of an array?

  let arr = [1, 1]; // fibonaci sequence
  let y = 0;
  while (y <= num) {
    let x = arr.slice(-2);
    y = x[0] + x[1];
    arr.push(y);
    console.log(y)
  }

Okay I 've got this far, seems I’ve got the fibonaci numbers now…
Question I have is why when Y reaches 5 it’s still pushing it when my while has a condition of y <= num?

What is your array when num=5?

[ 1, 1, 2, 3, 5, 8 ]

Ok. And the loop keeps repeating as long as y is less than or equal to num

am I missing something shouldn’t it stop at 3 then :S

Is 3 greater than 5?

ahh I see what u mean. Thanks

1 Like
function sumFibs(num) {
  let arr = [1, 1]; // fibonaci sequence
  let y = 0;
  let x = arr.slice(-2);
  while (y <= num) {
    x = arr.slice(-2);
    y = x[0] + x[1];
    arr.push(y);
  }
  const filtered = arr.filter(numb => {
    return numb % 2 !== 0
  })
  const summ = filtered.reduce((previousValue, currentValue) => {
    if (previousValue + currentValue >= num) {
      filtered.pop()
      return previousValue + currentValue;
    }
    return previousValue + currentValue;
  })
  console.log(filtered)
  const anotherSumm = filtered.reduce((a,b) => a+b);
  return anotherSumm
}

sumFibs(75024);

So, I had an issue which I just wrote but I deleted the post.
for some reason the code wasn’t updating on my IF statement inside the summ function, it was popping the last element but not summing up after the pop, it was summing before.
Could you explain to me why it didn’t work and I had to make another function? (anotherSumm)
I hope you understood what I wrote.