From cde3712686d497b761208a23266dc9c0e7b3ff4f Mon Sep 17 00:00:00 2001 From: Adrian Ancona Novelo Date: Sun, 18 Aug 2024 18:05:43 +0200 Subject: [PATCH] Nth fibonacci number exercise (#5) --- .../nth-fibonacci-number/answer/README.md | 24 +++++++++++++++ .../answer/nth-fibonacci-number.js | 30 +++++++++++++++++++ .../nth-fibonacci-number/exercise/README.md | 24 +++++++++++++++ .../exercise/nth-fibonacci-number.js | 12 ++++++++ 4 files changed, 90 insertions(+) create mode 100644 data-structures-and-algorithms/nth-fibonacci-number/answer/README.md create mode 100644 data-structures-and-algorithms/nth-fibonacci-number/answer/nth-fibonacci-number.js create mode 100644 data-structures-and-algorithms/nth-fibonacci-number/exercise/README.md create mode 100644 data-structures-and-algorithms/nth-fibonacci-number/exercise/nth-fibonacci-number.js diff --git a/data-structures-and-algorithms/nth-fibonacci-number/answer/README.md b/data-structures-and-algorithms/nth-fibonacci-number/answer/README.md new file mode 100644 index 0000000..f06d92a --- /dev/null +++ b/data-structures-and-algorithms/nth-fibonacci-number/answer/README.md @@ -0,0 +1,24 @@ +# Nth fibonacci number + +The Fibonacci numbers are the numbers in the following integer sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... + +Given a number n, print n-th Fibonacci Number. + +Examples: + +``` +Input: 0 +Output: 0 + +Input: 1 +Output: 1 + +Input: 2 +Output: 1 + +Input: 3 +Output: 2 + +Input: 9 +Output: 34 +``` diff --git a/data-structures-and-algorithms/nth-fibonacci-number/answer/nth-fibonacci-number.js b/data-structures-and-algorithms/nth-fibonacci-number/answer/nth-fibonacci-number.js new file mode 100644 index 0000000..f0c8d3d --- /dev/null +++ b/data-structures-and-algorithms/nth-fibonacci-number/answer/nth-fibonacci-number.js @@ -0,0 +1,30 @@ +const assert = require('assert'); + +function nthFibonacciNumber(n) { + if (n == 0) { + return 0; + } + + if (n == 1) { + return 1; + } + + let secondLast = 0; + let last = 1; + let current = null; + for (let i = 1; i < n; i++) { + current = secondLast + last; + secondLast = last; + last = current; + } + + return last; +} + +assert.equal(nthFibonacciNumber(0), 0); +assert.equal(nthFibonacciNumber(1), 1); +assert.equal(nthFibonacciNumber(2), 1); +assert.equal(nthFibonacciNumber(3), 2); +assert.equal(nthFibonacciNumber(9), 34); + +console.log("All assertions passed!"); diff --git a/data-structures-and-algorithms/nth-fibonacci-number/exercise/README.md b/data-structures-and-algorithms/nth-fibonacci-number/exercise/README.md new file mode 100644 index 0000000..f06d92a --- /dev/null +++ b/data-structures-and-algorithms/nth-fibonacci-number/exercise/README.md @@ -0,0 +1,24 @@ +# Nth fibonacci number + +The Fibonacci numbers are the numbers in the following integer sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... + +Given a number n, print n-th Fibonacci Number. + +Examples: + +``` +Input: 0 +Output: 0 + +Input: 1 +Output: 1 + +Input: 2 +Output: 1 + +Input: 3 +Output: 2 + +Input: 9 +Output: 34 +``` diff --git a/data-structures-and-algorithms/nth-fibonacci-number/exercise/nth-fibonacci-number.js b/data-structures-and-algorithms/nth-fibonacci-number/exercise/nth-fibonacci-number.js new file mode 100644 index 0000000..d6563ba --- /dev/null +++ b/data-structures-and-algorithms/nth-fibonacci-number/exercise/nth-fibonacci-number.js @@ -0,0 +1,12 @@ +const assert = require('assert'); + +function nthFibonacciNumber(n) { +} + +assert.equal(nthFibonacciNumber(0), 0); +assert.equal(nthFibonacciNumber(1), 1); +assert.equal(nthFibonacciNumber(2), 1); +assert.equal(nthFibonacciNumber(3), 2); +assert.equal(nthFibonacciNumber(9), 34); + +console.log("All assertions passed!");