diff options
Diffstat (limited to 'graphs/js/jestBasic')
| -rw-r--r-- | graphs/js/jestBasic/fibo.js | 29 | ||||
| -rw-r--r-- | graphs/js/jestBasic/fibo.test.js | 37 |
2 files changed, 66 insertions, 0 deletions
diff --git a/graphs/js/jestBasic/fibo.js b/graphs/js/jestBasic/fibo.js new file mode 100644 index 0000000..0a19955 --- /dev/null +++ b/graphs/js/jestBasic/fibo.js @@ -0,0 +1,29 @@ +function fibo(n) { + if (typeof n != "number" || isNaN(n)) { + return -1; + } + + if (n < 0) { + return -1; + } + + if (n === 0) { + return 0; + } + + let a = 1; + let b = 1; + let result = 1; + + for (let i = 2; i < n; i++) { + result = a + b; + b = a; + a = result; + } + + return result; +} + +module.exports = { + fibo, +}; diff --git a/graphs/js/jestBasic/fibo.test.js b/graphs/js/jestBasic/fibo.test.js new file mode 100644 index 0000000..6a97365 --- /dev/null +++ b/graphs/js/jestBasic/fibo.test.js @@ -0,0 +1,37 @@ +const { fibo } = require("./fibo"); + +describe("basic value", () => { + test("fibo of 0", () => { + expect(fibo(0)).toBe(0); + }); + test("fibo of 1", () => { + expect(fibo(1)).toBe(1); + }); + test("fibo of 2", () => { + expect(fibo(2)).toBe(1); + }); + test("fibo of 4", () => { + expect(fibo(4)).toBe(3); + }); + test("fibo of 19", () => { + expect(fibo(19)).toBe(4181); + }); +}); + +describe("errors", () => { + test("fibo of -1", () => { + expect(fibo(-1)).toBe(-1); + }); + test("fibo of null", () => { + expect(fibo(null)).toBe(-1); + }); + test("fibo of undefined", () => { + expect(fibo(undefined)).toBe(-1); + }); + test("fibo of a string", () => { + expect(fibo("uwu adrien")).toBe(-1); + }); + test("fibo of NaN", () => { + expect(fibo(NaN)).toBe(-1); + }); +}); |
