Quotes help make search much faster. Example: "Practice Makes Perfect"

Saturday, May 13, 2017

FreeCodeCamp Sum All Odd Fibonacci Numbers Sample Solution


function generateFibSeq(num){
  var arr=[1,1];
  while(num > arr[arr.length-1]){
    if(arr[arr.length-1]+arr[arr.length-2] > num){
      break;
    }
    arr.push(arr[arr.length-1]+arr[arr.length-2]);
  }
  return arr;
}

function sumFibs(num) {
  if(num===1){
    return 1;
  }
  var fibArr = generateFibSeq(num);
 
  var sum = 0;
  for(var i in fibArr){
    if(fibArr[i]%2 !== 0){
      sum += fibArr[i];
    }
  }
  return sum;
}

sumFibs(75025);

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts