0

I'm trying to solve a challenge at FreeCodeCamp. but I'm trying to code in a more functional way. so the challenge is to sum all of the odd fibonacci numbers less than or equal a given number. I tried to avoid for loops and tried to use the accepted answer from this topic: How do I replace while loops with a functional programming alternative without tail call optimization?

But I cant figure out why this doesn't work. what am I missing? I'm very new at this so sorry if the answer is too easy.

const sumFibs = num => {
  const repeat = n => f => x => {
    let m = n

    while (m > num - x.length) {
      if (m === 0)
        return x

      else
        (m = m - 1, x = f(x))
    }
  }
  const gadzillionTimes = repeat(num)

  const add1 = x => {
    return x[0] + x[1] <= num ? [x[0] + x[1]].concat(x) : x;
  }

  const allFibonaci = gadzillionTimes(add1)([1, 1]);
  return allFibonaci.filter(x => x % 2 !== 0).reduce((a, b) => a + b);
}
sumFibs(4000000);
VLAZ
  • 26,331
  • 9
  • 49
  • 67
samvision
  • 9
  • 1
  • What's the expected and the actual behavior? Have you tried to debug the code? – Thomas Sablik Dec 29 '20 at 12:43
  • `repeat` doesn't return anything when it goes into the `else`. – VLAZ Dec 29 '20 at 12:43
  • `repeat doesn't return anything when it goes into the else` This is the expected behaviour if you don't add a `return` there. – emi Dec 29 '20 at 12:48
  • well, when I change the while condition to 'true'. it does work but its slow. I'm trying to limit the numbers of loops so it wouldent continiue looping when the fibonaci array is complete – samvision Dec 29 '20 at 12:50
  • `(m = m - 1, x = f(x))` : this is a very strange syntax. I suggest to use something more comprehensible if you are looking for help, like `{ m = m - 1; x = f(x); }`. Also, here you are modifying a parameter. While it can be done, it is also confusing. I suggest to rethink this. – emi Dec 29 '20 at 12:51
  • you're apparently trying to repeat 4M times, instead of only summing the (odd) _values_ up to 4M. – Alnitak Dec 29 '20 at 13:22

2 Answers2

0

There is a lot going on with your code and I want to show you a way to accomplish the same thing without using for loops.

function sumFibs(num) {
let a = 1;
let b = 1;
let sum = 1;

while (b <= num) {
if (b % 2 ==1) {
sum += b;}

let temp = b;
b = a + b;
a = temp;
  }
return sum;

No worries about being new to the scene. You are on the right track by trying things out and sometimes there are shorter ways to accomplish the same goal. Please let me know if you have any questions!

LukeReact
  • 59
  • 1
  • 2
  • 13
0

Fibonacci's can be described declaratively with a recursive function.

function nthFib(n) {
  return n<2 ? n : nthFib(n-1) + nthFib(n-2)
}

An array of n fibonacci's can be described as a map over the counting numbers, where each element is the nth fibonacci

function fibs(n) {
  return [ ...Array(n).keys() ].map(i => nthFib(i))
}

Odd fibonacci's can be expressed as filtered fibonacci's (where modulo 2 is 1)

const oddFibs = fibs(n).filter(f => f%2)

A sum can be expressed declaratively with reduce()

const sum = oddFibs.reduce((acc, v) => acc + v, 0)
danh
  • 62,181
  • 10
  • 95
  • 136