Fibonacci number using NodeJS


'use strict';

const readline  = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

//get fibonacci number using recursive function.
let fib = (n) => {
  if(n <= 2) {
    return 1
  } else {
    return fib(n - 1) + fib(n - 2);
  }
};

//read the input from console
rl.question('Please enter number ', (readn) => {
  

  console.log(fib(readn));

  rl.close();
});


More about nodejs and recursion at https://kostasbariotis.com/node-js-recursion/
More recursion patterns for nodejs at : http://metaduck.com/01-asynchronous-iteration-patterns.html

Popular posts from this blog

npm refusing to install as a dependency of itself

How to call javascript functions from VB.NET and using WebBrowser Control