From d5f866c3be7a1d60073951523cfc054d822b86bf Mon Sep 17 00:00:00 2001 From: merelymyself <88221256+merelymyself@users.noreply.github.com> Date: Thu, 21 Apr 2022 18:03:49 +0800 Subject: [PATCH 1/7] Optimised the factorial function. There was previously an unnecessary check for if the number was 0 or 1. --- Maths/WhileLoopFactorial.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Maths/WhileLoopFactorial.js b/Maths/WhileLoopFactorial.js index 7e04bbafa1..f539196730 100644 --- a/Maths/WhileLoopFactorial.js +++ b/Maths/WhileLoopFactorial.js @@ -1,15 +1,13 @@ /* - author: Theepag + author: Theepag, optimised by merelymyself */ export const factorialize = (num) => { - // Step 1. variable result to store num - let result = num - // If num = 0 OR 1, the factorial will return 1 - if (num === 0 || num === 1) { return 1 } + // Step 1. Handles cases where num is 0 or 1, by returning 1. + let result = 1 // Step 2. WHILE loop while (num > 1) { - num-- // decrement 1 at each iteration result = result * num // or result = result * num; + num-- // decrement 1 at each iteration } // Step 3. Return the factorial return result From a6767bac90a573f953fd705c999a014c06c0443f Mon Sep 17 00:00:00 2001 From: merelymyself <88221256+merelymyself@users.noreply.github.com> Date: Thu, 21 Apr 2022 18:12:14 +0800 Subject: [PATCH 2/7] Create WhileLoopFactorial.test.js The test was not present previously. --- Maths/test/WhileLoopFactorial.test.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Maths/test/WhileLoopFactorial.test.js diff --git a/Maths/test/WhileLoopFactorial.test.js b/Maths/test/WhileLoopFactorial.test.js new file mode 100644 index 0000000000..5918632490 --- /dev/null +++ b/Maths/test/WhileLoopFactorial.test.js @@ -0,0 +1,21 @@ +import { factorialize } from '../WhileLoopFactorial' + +test('Testing on 3!', () => { + const three_factorial = factorialize(3) + expect(three_factorial).toBe(6) +}) + +test('Testing on 7!', () => { + const seven_factorial = factorialize(7) + expect(seven_factorial).toBe(5040) +}) + +test('Testing on 0!', () => { + const zero_factorial = factorialize(0) + expect(zero_factorial).toBe(1) +}) + +test('Testing on 12!', () => { + const twelve_factorial = factorialize(12) + expect(twelve_factorial).toBe(479001600) +}) From de12e1820c1a7b5f0b9dba7e1a59d2ab106e7fc8 Mon Sep 17 00:00:00 2001 From: merelymyself <88221256+merelymyself@users.noreply.github.com> Date: Thu, 21 Apr 2022 18:58:43 +0800 Subject: [PATCH 3/7] result *= num --- Maths/WhileLoopFactorial.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/WhileLoopFactorial.js b/Maths/WhileLoopFactorial.js index f539196730..943fabd8d6 100644 --- a/Maths/WhileLoopFactorial.js +++ b/Maths/WhileLoopFactorial.js @@ -6,7 +6,7 @@ export const factorialize = (num) => { let result = 1 // Step 2. WHILE loop while (num > 1) { - result = result * num // or result = result * num; + result *= num // or result = result * num; num-- // decrement 1 at each iteration } // Step 3. Return the factorial From acce146169e5b484740cde1f08df0021bdbdbee7 Mon Sep 17 00:00:00 2001 From: merelymyself <88221256+merelymyself@users.noreply.github.com> Date: Thu, 21 Apr 2022 19:01:36 +0800 Subject: [PATCH 4/7] Update WhileLoopFactorial.test.js --- Maths/test/WhileLoopFactorial.test.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Maths/test/WhileLoopFactorial.test.js b/Maths/test/WhileLoopFactorial.test.js index 5918632490..de53ee9491 100644 --- a/Maths/test/WhileLoopFactorial.test.js +++ b/Maths/test/WhileLoopFactorial.test.js @@ -1,21 +1,17 @@ import { factorialize } from '../WhileLoopFactorial' test('Testing on 3!', () => { - const three_factorial = factorialize(3) - expect(three_factorial).toBe(6) + expect(factorialize(3)).toBe(6) }) test('Testing on 7!', () => { - const seven_factorial = factorialize(7) - expect(seven_factorial).toBe(5040) + expect(factorialize(7)).toBe(5040) }) test('Testing on 0!', () => { - const zero_factorial = factorialize(0) - expect(zero_factorial).toBe(1) + expect(factorialize(0)).toBe(1) }) test('Testing on 12!', () => { - const twelve_factorial = factorialize(12) - expect(twelve_factorial).toBe(479001600) + expect(factorialize(12)).toBe(479001600) }) From 985d0c583514b70b4f5d7614b7bcd10e7b0402a7 Mon Sep 17 00:00:00 2001 From: merelymyself <88221256+merelymyself@users.noreply.github.com> Date: Thu, 21 Apr 2022 20:42:15 +0800 Subject: [PATCH 5/7] testFactorial function --- Maths/test/WhileLoopFactorial.test.js | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/Maths/test/WhileLoopFactorial.test.js b/Maths/test/WhileLoopFactorial.test.js index de53ee9491..1f8c9a8749 100644 --- a/Maths/test/WhileLoopFactorial.test.js +++ b/Maths/test/WhileLoopFactorial.test.js @@ -1,17 +1,12 @@ import { factorialize } from '../WhileLoopFactorial' -test('Testing on 3!', () => { - expect(factorialize(3)).toBe(6) -}) - -test('Testing on 7!', () => { - expect(factorialize(7)).toBe(5040) -}) - -test('Testing on 0!', () => { - expect(factorialize(0)).toBe(1) -}) - -test('Testing on 12!', () => { - expect(factorialize(12)).toBe(479001600) -}) +function testFactorial(n, expected) { + test('Testing on ' + n + '!', () => { + expect(factorialize(n)).toBe(expected) + }) +} + +testFactorial(3, 6) +testFactorial(7, 5040) +testFactorial(0, 1) +testFactorial(12, 479001600) From 2d40205bbc116c2ae57a18154479bd79dc619ad1 Mon Sep 17 00:00:00 2001 From: merelymyself <88221256+merelymyself@users.noreply.github.com> Date: Thu, 21 Apr 2022 23:28:13 +0800 Subject: [PATCH 6/7] Space for formatting. --- Maths/test/WhileLoopFactorial.test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Maths/test/WhileLoopFactorial.test.js b/Maths/test/WhileLoopFactorial.test.js index 1f8c9a8749..460cb90358 100644 --- a/Maths/test/WhileLoopFactorial.test.js +++ b/Maths/test/WhileLoopFactorial.test.js @@ -1,12 +1,12 @@ import { factorialize } from '../WhileLoopFactorial' function testFactorial(n, expected) { - test('Testing on ' + n + '!', () => { - expect(factorialize(n)).toBe(expected) + test ('Testing on ' + n + '!', () => { + expect (factorialize (n)).toBe (expected) }) } -testFactorial(3, 6) -testFactorial(7, 5040) -testFactorial(0, 1) -testFactorial(12, 479001600) +testFactorial (3, 6) +testFactorial (7, 5040) +testFactorial (0, 1) +testFactorial (12, 479001600) From 1956597c55b19a0d0d2c4099d90f1b459ea8faba Mon Sep 17 00:00:00 2001 From: merelymyself Date: Fri, 22 Apr 2022 00:05:07 +0800 Subject: [PATCH 7/7] should fix the formatting issues. I was having trouble with npx standard, so I just used the online verifier at https://standardjs.com/demo.html --- Maths/test/WhileLoopFactorial.test.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Maths/test/WhileLoopFactorial.test.js b/Maths/test/WhileLoopFactorial.test.js index 460cb90358..6cec49f36d 100644 --- a/Maths/test/WhileLoopFactorial.test.js +++ b/Maths/test/WhileLoopFactorial.test.js @@ -1,12 +1,12 @@ import { factorialize } from '../WhileLoopFactorial' -function testFactorial(n, expected) { - test ('Testing on ' + n + '!', () => { - expect (factorialize (n)).toBe (expected) +function testFactorial (n, expected) { + test('Testing on ' + n + '!', () => { + expect(factorialize(n)).toBe(expected) }) } -testFactorial (3, 6) -testFactorial (7, 5040) -testFactorial (0, 1) -testFactorial (12, 479001600) +testFactorial(3, 6) +testFactorial(7, 5040) +testFactorial(0, 1) +testFactorial(12, 479001600) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy