Last active
September 22, 2018 17:05
-
-
Save Underwaterr/443bddb501935847ae5668aa3e910dda to your computer and use it in GitHub Desktop.
Fibonacci Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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