Last active
January 24, 2021 06:48
-
-
Save keidarcy/02f4216978a9b049054466428cca9129 to your computer and use it in GitHub Desktop.
Find out the Sequencing of Fibonacci numbers
This file contains hidden or 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
const fibonacci = (num) => { | |
if (Number.isNaN(+num)) { | |
return 'not a number'; | |
} | |
let result = 0; | |
let tmp = 0; | |
let fiArr = [1, 1]; | |
for (let i = 1; i < num; i++) { | |
if (num === 1) { | |
result = 0; | |
break; | |
} | |
tmp = fiArr[i] + fiArr[i - 1]; | |
fiArr.push(tmp); | |
if (num === tmp) { | |
result = i + 1; | |
break; | |
} | |
} | |
console.log(fiArr); | |
if (!result) return `${num} in not in fibonacci numbers.`; | |
return `the sequence of ${num} fibonacci numbers is ${result}`; | |
}; | |
console.log(fibonacci(+process.argv[2])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to execute it
wget -qO - https://gist.githubusercontent.com/keidarcy/02f4216978a9b049054466428cca9129/raw/51d999239ab80361c300c811a1114896ffddf501/fibonacci.js | node /dev/stdin 55