Q:

Sum of all odd fibonacci numbers upto n

I am stuck in summing up to number 10. Can't get it right.
What i am getting is the total sum from 10 digits (after filtering odd only 7). Where should i make <= num ?

function sumFibs(num) {
  var fib = [1, 1];
  for (var i = 2; i < num; i++) {
    var next = fib[i - 1] + fib[i - 2];
    var fibi = fib.push(next);
  }

  return fib.filter(function(a) {
      return (a % 2 != 0);
    })
    .reduce(function(a, z) {
      return a + z;
    })
}
console.log(sumFibs(10));

Expected output 10, but getting 99

A:

Add a < num to your filter callback test, so you get a % 2 && a < num

function sumFibs(num) {
 var fib = [0, 1];
 for (var i = 2; i < num; i++) {
  var next = fib[i - 1] + fib[i - 2];
  var fibi = fib.push(next);
 }
 return fib.filter(function (a) {
  return a % 2 && a < num;
 }).reduce(function (a, z) {
  return a + z;
 }, 0);
}

console.log(sumFibs(0))
console.log(sumFibs(1))
console.log(sumFibs(10))
console.log(sumFibs(9000))

You don't need to use array at all if you need only sum of those numbers

function sumFibs(num) {
  if(num <= 1) return 0;
  var a = 0, b = 1, sum = a + b;
  while(true) {
    var next = a + b;
    if(next >= num) {
      break;
    }
    if(next % 2) {
      sum += next;
    }
    a = b;
    b = next;
  }
  return sum
}
console.log(sumFibs(0))
console.log(sumFibs(1))
console.log(sumFibs(10))
console.log(sumFibs(9000))

