# Help with initial inquiry into Sum All Odd Fibonacci Numbers

Help with initial inquiry into Sum All Odd Fibonacci Numbers
0
#1

Tell us what’s happening:
Below I have my initial idea about how to approach this challenge. I have some suspicions about my approach which I’m calling “weak points”. I would appreciate it if someone would address my concerns as baseless or on track. If you see anything grossly out of order in my approach, then I would appreciate guidance in this matter, but my main desire is to find out if my weak points are valid. Thank you.

Weak points:
I have to perform two different forms of math: that which creates the sequence and that which adds together the odd integers.
a. I have a suspicion that these could be combined into a single sequence under filter - am I mistaken?
b. I have a suspicion that 2. should use a for loop instead, specifying the limit that numbers can be looped through and THEN passing those elements into function that creates the Fibonacci sequence. I feel like a simple if statement is not enough.

1. Create empty array
2. Use if statement: for all positive integers less than or equal to value of variable num, create alogrithm addition corresponding to fibonacci sequence and push numbers this creates into empty array
3. Use filter() to draw odd numbers out of that array that are less than or equal to num
5. Return that sum

Thank you again.

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

0 Likes

#2

Consider what the Fibonacci sequence is, instead of checking all numbers, you can make it easier if you use the definition of Fibonacci sequence to create the sequence.

This would make you iterate over the array twice… can you do it with one iteration?
If the array is not wrong this would give the correct answer, but if something could be made to need less resources…

0 Likes

#3

I think that I’m pretty close so I’m going to preemptively blur out my code, but this is not to say that it’s correct, but I’m not passing several of the tests for the bigger numbers. I’ve console.logged them and I don’t understand how I’m getting it wrong.

I think that I’m probably going wrong with pushing i++ into array …I’m not sure why I think this, but it just doesn’t seem like a stable enough element to be pushing into the array and it seems like I lucked out with the few smaller numbers I did pass but that in larger numbers its glaring holes are revealed.
If I’m incorrect about this assumption, then I would desire swift and severe guidance in order to change my trajectory.
Thank you.

``````
function sumFibs(num) {
//create array container to hold Fibonacci values.  Because all Fibonacci sequence will inevitably include two 1 values, insert an initial 1 into array
let array = [1];
//run for loop with i = 1 as it will be the second instance of 1, but also it will help keep the array odd
for(let i = 1; i <= num; i++){
//push the incrementation of these odd values into array
array.push(i++);
}
//use reduce method to add all these together and assign it to container sum
const sum = array.reduce((a,b) => a + b);
console.log(sum)
return sum;

}

sumFibs(4);
``````
0 Likes

#4

Start with the definition of fibonacci numbers. You’re pushing consecutive integers on an array isn’t what you need here.

0 Likes

#5

What’s the Fibonacci sequence? How do you calculate it? Explain that to us

1 Like

#6

I’ve added in what I believe is a good formula for determining Fibonacci sequence numbers. I am still passing the same number of tests as before when I was just iterating through the sequence. I’m not JUST iterating through it now. I have added conditions, BUT I’m obviously not really getting something essential if I’m still at the same number of tests.
When I run other tests, with 1000 for example
My questions:
Am I not addressing any of the above questions? Is my current approach counter-productive to resolving this problem?
OR
Am I making slight progress BUT missing something in the syntax that would make the solution universal as opposed to localized to a localized condition?

``````
function sumFibs(num) {
//create container for Fibonacci sequence with initial 1 included (as it is a given in the sequence)
let array = [1];
//iterate through num all elements that are less than or equal to value of num
for(let i = 1; i <= num; i++){
//if elements are greater than 0 and not even then they are passed through fibonacci formula and pushed onto array
if(i > 0 && i%2 !== 0){
((i-1) + (i-2)) && array.push(i)
}
console.log(array)
}
//once all element are in array they are added together and this sum is assigned to num
num = array.reduce((a, b)=> a+b);

return num;
}

sumFibs(1000);
``````
0 Likes

#7

You are not using the Fibonacci sequence, your array is just odd numbers
Can you give me the definition of Fibonacci sequence?

This part is just pushing odd `i`s to the array
You are not applying any formula that create the Fibonacci sequence

Can I suggest you to divide the algorithm in two separe algorithms?

1. Create an array with Fibonacci sequence numbers that are lower than `n`
2. Sums odd numbers in an array
1 Like

#8

I figured it out. However, I want to make sure that I’m explaining my code correctly, using terminology that’s accurate and not filled with incorrect terminology that may lead me astray further in my studies. Would someone mind reading over my comments and adjudicating them?
If anyone is willing to do this, I would really appreciate it. Thank you all.

``````
function sumFibs(num) {
//create variables: empty array[array], two core variables for opening integers[m,n] of sequence, variable for holding temporary sum [temp] to be used for continuing sequence
let array = [];
let m = 0;
let n = 1;
let temp;
//use while loop until variable m is greater than value specified by num
while(m <= num){
//smaller number is temporary and will be replaced as loop works toward completion
temp = m;
//variable m is the sum of itself and next variable
m = m+n;
//n, being the larger number will be next temporary variable to be added to sum of preceding variables
n = temp;
//push this variable onto the empty array
array.push(n)
}
console.log(array)
//assign array to itself filtered to contain only non-even numbers
array = array.filter(x => x%2!==0);
console.log(array)
//reassign variable num to array contents that have been added together using reduce method
num = array.reduce((a,b) => a+b);
console.log(num);

return num;
}

sumFibs(1000);
``````
0 Likes

#9

Last comment: you are assigning something to `num`, `num` is not assigned, `num` is being assigned a value

If you want a hint to make it easier to read:

This is only my opinion, you can decide that your code is fine like that and stop there.
You can totally start with an array that is not all empty, for example by definition the series starts with `[1, 1]`, you could also use `array[array.lenght - 2]` and `array[array.length - 1]` to reference those two.
You can also avoid the two assignments st the bottom and just concat everything in the return statement

``````return array.filter(...).reduce(...)
``````

Extra thing: you can do it using only reduce, to avoid iterating twice over the array if you want to make it more performing. You just have to think carefully on what you need inside the reduce method to also do the filtering.

0 Likes