summaryrefslogtreecommitdiff
path: root/graphs/js/jestBasic
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
commitc9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch)
tree3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/js/jestBasic
add: graphs et rushs
Diffstat (limited to 'graphs/js/jestBasic')
-rw-r--r--graphs/js/jestBasic/fibo.js29
-rw-r--r--graphs/js/jestBasic/fibo.test.js37
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);
+ });
+});