From b400290186cae92767147042dd72881145673def Mon Sep 17 00:00:00 2001 From: theepag Date: Tue, 20 Oct 2020 23:07:14 +0530 Subject: [PATCH 1/2] Added while loop factorial --- Maths/WhileLoopFactorial.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Maths/WhileLoopFactorial.js diff --git a/Maths/WhileLoopFactorial.js b/Maths/WhileLoopFactorial.js new file mode 100644 index 0000000000..ce1ba17be3 --- /dev/null +++ b/Maths/WhileLoopFactorial.js @@ -0,0 +1,20 @@ +/* + author: Theepag + */ +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 2. WHILE loop + while (num > 1) { + num--; // decrement 1 at each iteration + result = result * num; // or result = result * num; + } + // Step 3. Return the factorial + return result; +} +//test +console.log(factorialize(5)); +console.log(factorialize(4)); \ No newline at end of file From 4e680d70879c4fd4f8f9c3d0e0d8e5bea3c48386 Mon Sep 17 00:00:00 2001 From: vinayak Date: Sat, 31 Oct 2020 12:34:54 +0530 Subject: [PATCH 2/2] Update WhileLoopFactorial.js --- Maths/WhileLoopFactorial.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/Maths/WhileLoopFactorial.js b/Maths/WhileLoopFactorial.js index ce1ba17be3..58501697f6 100644 --- a/Maths/WhileLoopFactorial.js +++ b/Maths/WhileLoopFactorial.js @@ -2,19 +2,18 @@ author: Theepag */ 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 2. WHILE loop - while (num > 1) { - num--; // decrement 1 at each iteration - result = result * num; // or result = result * num; - } - // Step 3. Return the factorial - return result; + // 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 2. WHILE loop + while (num > 1) { + num-- // decrement 1 at each iteration + result = result * num // or result = result * num; + } + // Step 3. Return the factorial + return result } -//test -console.log(factorialize(5)); -console.log(factorialize(4)); \ No newline at end of file +// test +console.log(factorialize(5)) +console.log(factorialize(4))