this code right here does work, but i don't quite understand why.
if i remove the interior return statement and keep only the one at the end, it will return the correct sum (44) for console.log(evenFibonnacisSum(100)), but undefined for console.log(evenFibonnacisSum(4000000)).
if i remove the exterior return statement and keep the one inside the if statement, it will return undefined for console.log(evenFibonnacisSum(100)) but the correct sum (46000000 or so) for console.log(evenFibonnacisSum(4000000)).
function evenFibonnacisSum(upperlimit){
let evenSum = 0
let seq = [0, 1]
for(i=0; i<=upperlimit; i++) {
if(evenSum<upperlimit) {
let next = seq[i]+seq[i+1];
seq.push(next)
if(seq[i]%2 === 0 && seq[i]<upperlimit) {
evenSum += seq[i]
}
} else {
return evenSum
}
}
return evenSum
}
console.log(evenFibonnacisSum(100))
console.log(evenFibonnacisSum(4000000))
it's not a huge problem, but it's really unintuitive. anyone have any insight into why this might be?