Basic JavaScript Algorithm, Fibonacci series -
sum odd numbers of fibonacci series , including given number.
i can't figure out syntax problem i'm having. problem, loop, loop ends when greater or equal value of num
, instead want end the, generated values of start
.
is there way make work?
function sumfibs(num) { var odd = [1]; // odd numbers of fibonacci series var start = [0,1]; // fibonacci series // generating series , filtering out odd numbers for(i=1;i<num;i++) { var sum = 0; sum = start[i] + start[i-1]; start.push(sum); if(sum%2 != 0) { odd.push(sum); } } // generating sum of odd numbers var main = 0; // sum of odd numbers for(i=0;i<odd.length;i++) { main += odd[i] } console.log(start);console.log(odd);return main } sumfibs(4);
it should if understand correctly
while(start.length<num) { code here }
Comments
Post a Comment