for some reason, my code keeps failing the last check. I’ve tried to fix the code a few times but can’t tell what I’m doing wrong. I also don’t know how I will be able to fix something like this in the future I tried console logging the different arrays I placed to see where the mistake was but I’m completely lost.
**Your code so far**
function sumFibs(num) {
let fibn = [];
let oddFib = [];
let sum = [];
fibn[0] = 0;
fibn[1] = 1;
if(num < 0){
return num = undefined;
}
if (num === 0){
return num;
}
if (num === 1){
return num;
}
if (num >2){
// this loop creates the fibonacci array up to the number submited as the argument
for ( let i = 2; i <= num; i++){
fibn[i] = fibn[i - 2] + fibn[i - 1];
}
}
// This loop is responsible for finding all odd number in the fibonacci array and pushes them to a new array to be sorted
for (let i = 0; i < fibn.length; i++){
if (fibn[i] % 2 !== 0 ){
oddFib.push(fibn[i]);
}
}
// this loop will push all the number in the odd number fibbonacci array to a sum array that will be reduced in the return statement
for (let i = 0; i < oddFib.length; i ++){
if (oddFib[i] < num)
{
sum.push(oddFib[i]);
}
}
return sum.reduce((a,b) => a + b);
}
console.log(sumFibs(75025));
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36
it goes way too far making far too many calculations because I have that for loop set to number. I’m not sure what to set that length to so that it stays constant but can still take all the input parameters for the test
Ok, lets fix that so that our debugging effort are easier.
Given a positive integer num , return the sum of all odd Fibonacci numbers that are less than or equal to num .
Looking at this requirement, it looks like the individual numbers in the array all shouldn’t be bigger than num, not the index of these numbers.
So, you are making “the first 10 Fibonacci numbers” instead of “the Fibonacci numbers less than 10”.
That’s what this line says:
for (let i = 2; i <= num; i++) {
So we either need to change this for loop condition or change the type of loop we are using so that we keep adding Fibonacci numbers until we find one that’s bigger than num.
I’d also use a smaller test case that shows the bug. Here’s a new test for you:
let i = 2;
while (i <= num){
fibn[i] = fibn[i - 2] + fibn[i - 1];
i++;
}
but that still makes it fail on my very last test for 75025. Before posting about it again I tried to think it through and realized if it isn’t registering the last number to push onto my odd array there must be something wrong with my odd loop push and it was set to < instead of <= . typed that char right in and the program worked. Thanks for your help so much. I realize my answer :
function sumFibs(num) {
let fibn = [];
let oddFib = [];
let sum = [];
fibn[0] = 0;
fibn[1] = 1;
if(num < 0){
return num = undefined;
}
if (num === 0){
return num;
}
if (num === 1){
return num;
}
if (num >2){
// loop for fibbonacci numbers that are less than or equal to number argument
let i = 2;
while (i <= num){
fibn[i] = fibn[i - 2] + fibn[i - 1];
i++;
}
}
// This loop is responsible for finding all odd number in the fibonacci array and pushes them to a new array to be sorted
for (let i = 0; i < fibn.length; i++){
if (fibn[i] % 2 !== 0 ){
oddFib.push(fibn[i]);
}
}
// this loop will push all the number in the odd number fibbonacci array to a sum array that will be reduced in the return statement
for (let i = 0; i < oddFib.length; i ++){
if (oddFib[i] <= num)
{
sum.push(oddFib[i]);
}
}
return sum.reduce((a,b) => a + b);
}
console.log(sumFibs(75025));
what should i be thinking about when ttrying to write more efficiently?
That’s still the same issue unfortunately. You need new fibs when fib[i] < num
This is a great candidate for one of my favorite “rules of thumb” for efficient/simple code:
only store values if you need them more than once
You are only using the Fibonacci values once, to sum up the odd ones. If you get the loop condition above correct, then you can start putting more of your logic into that loop, like summing the odd values together.