While it is good you were able to get the tests to pass, you failed the sumFibs(4000000)
test due to a timeout. That means something in your code is taking too long to run.
How may I correct that?
Your issue is still in your for
loop. Your condition i<num
is telling the function to loop four MILLION times on the sumFib(4000000)
test. Check my reply above where I mentioned changing that.
If I set i<10, I will run 9 times. Bur if I set i<=10, I will make loop until it is the same as num. We want that becasue it is the length of our array. Is this right?
i<=num
Nope. sumFib(10)
should start by finding all of the Fibonacci numbers that are smaller than 10, NOT the first 10 numbers.
You need to change your i<num
to something that checks if the last number in numArr
is bigger than num
(which is 10, in my example).
Just so you know, the rest of your code works! You are almost there! Once you get this last little piece correct, you will pass the tests.
I am thinking about it. I think is i<=numArr[numArr.length-1]
You are getting closer!
i<=numArr[numArr.length=1]
is comparing the number of times the loop runs with the last value of the array.
You want to compare the value of num
with the last value of the array.
So numArr[numArr.length=1]
is correct. But how should it be compared to num
?
num<=numArr[numArr.length-1]
That tells your loop to run only when the last number in your array is larger than num
. Flip it around, replace i<num
with it, and run your tests
My computer stops working
You didn’t flip it around.
num>=numArr[numArr.length-1]
Now the last one is not working
What does your for (...) {
line look like?
function sumFibs(num) {
let numArr = [1, 1];
for(let i=0;num>=numArr[numArr.length-1];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);
It looks like you accidentally changed line 18.
let lessThanNumArr=oddNumArr.filter(n=>n<num)
The (n=>n<num)
used to be (n=>n<=num)
. Change it back to that and it should work again.
You have shared your knowledge and your time with me. I am grateful with you eternally, sir.
function sumFibs(num) {
let numArr = [1, 1];
for(let i=0;num>=numArr[numArr.length-1];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(75025);
Hurray! You got it!!!
I am more than happy to help! Now that you’ve passed this challenge, make sure you celebrate your achievement! It’s important to reward yourself for completing something that has been testing your skills!
It has been daring for me. You helped me to realize how many weaknesses I still have and I need to work on them. I will not disappoint you. Your time and dedication are invaluable to me.
We are all in this together, my friend! I am always happy to help support my fellow FCCers!
Having weaknesses is natural. I struggled a LOT with React in the front end libraries curriculum. But keep asking questions, keep your mind open to learning, and don’t give up - you can turn those weaknesses into strengths!