Javascript: Path to eliminating If/else
Ken Aguilar
244

isZero and isOne are very similar, they could be written as

const is = (n, x) => n === x;

then you could get rid of or function if you extend it to

const isOneOf = (n, ...values) => values.indexOf(n) > -1;

the same is of course true for oneOrZero which when thought through can also be expressed as returning n, right?

resulting in the following

const fibonacci = n => isOneOf(n, 0, 1)
? n
: fibonacci(n - 1) + fibonacci(n - 2);

do you know of any reason not to go into this direction?

Or did I miss something?

Like what you read? Give Christian Bewernitz a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.