Skip to content

Instantly share code, notes, and snippets.

@Underwaterr
Last active September 22, 2018 17:05
Show Gist options
  • Save Underwaterr/443bddb501935847ae5668aa3e910dda to your computer and use it in GitHub Desktop.
Save Underwaterr/443bddb501935847ae5668aa3e910dda to your computer and use it in GitHub Desktop.
Fibonacci Calculator
// What a classic!
function fibonacci(n) {
if(n == 1) return 1
else if (n == 2) return 1
else return (fibonacci(n-1) + fibonacci(n-2))
}
// Uses Node's "readline" module for input
// https://nodejs.org/api/readline.html
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
readline.question("Fibonacci number? ", (userResponse)=> {
let calculateToNumber = Number(userResponse)
if(isNaN(calculateToNumber)) console.log("Must be a number")
else if (calculateToNumber <= 0) console.log("Must be a number greater than 0")
else console.log(fibonacci(calculateToNumber))
readline.close()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment