Skip to content

Commit 041c60a

Browse files
committed
Create Multiplicative Persistence
1 parent 6dfec2e commit 041c60a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Multiplicative Persistence

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/***************************************************************************************
2+
* *
3+
* CODERBYTE BEGINNER CHALLENGE *
4+
* *
5+
* Multiplicative Persistence *
6+
* Using the JavaScript language, have the function FirstFactorial(num) take the num *
7+
* parameter being passed and return the factorial of it (ie. if num = 4, *
8+
* return (4 * 3 * 2 * 1)). For the test cases, the range will be between 1 and 18. * *
9+
* *
10+
* SOLUTION *
11+
* You can either use an iterative or recursive function to solve this challenge. *
12+
* I am going to use an interative function. I am going to start with a value of 1 *
13+
* for my total and then keep multiplying it by the next number until I reach num. *
14+
* *
15+
* Steps for solution *
16+
* 1) Initialize vars sum and loop *
17+
* 2) Loop from 2 to num and multiple tot by num to get new tot. *
18+
* 3) Return tot for answer. *
19+
* *
20+
***************************************************************************************/
21+
22+
function MultiplicativePersistence(num) {
23+
24+
var sum, loop = 0;
25+
var val1 = num.toString(10).split("");
26+
27+
while( val1.length > 1 ) {
28+
sum = 1;
29+
val1.forEach( function(number) {
30+
sum = sum * number;
31+
});
32+
val1 = sum.toString(10).split("");
33+
loop++;
34+
} ;
35+
36+
return loop;
37+
38+
}

0 commit comments

Comments
 (0)