Skip to content

Commit 23fc375

Browse files
committed
✨ challenge-51 solution added
1 parent 2829924 commit 23fc375

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

2022/51-reto-random/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Reto 51: Random
2+
3+
## Enunciado
4+
5+
Escribe una función, `persistencia`, que tome un parámetro positivo `num` y devuelva su persistencia multiplicativa, que es el número de veces que debes multiplicar los dígitos en `num` hasta llegar a un solo dígito.
6+
7+
Por ejemplo **(Entrada --> Salida):**
8+
9+
```bash
10+
39 --> 3 (porque 3*9 = 27, 2*7 = 14, 1*4 = 4 y 4 tiene un solo dígito)
11+
999 --> 4 (porque 9*9*9 = 729, 7*2*9 = 126, 1*2*6 = 12 y finalmente 1*2 = 2)
12+
4 --> 0 (porque 4 ya es un número de un dígito)
13+
```

2022/51-reto-random/solution.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const persistence = (num) => {
2+
let timesMultiplied = 0;
3+
let numbers = num.toString().split('');
4+
while (numbers.length > 1) {
5+
let resultTemp = 1;
6+
numbers.forEach((item) => (resultTemp *= Number(item)));
7+
timesMultiplied++;
8+
numbers = resultTemp.toString().split('');
9+
}
10+
return timesMultiplied;
11+
};
12+
13+
module.exports = persistence;

2022/51-reto-random/solution.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const persistence = require('./solution');
2+
3+
describe('Challenge 51: Reto random', () => {
4+
const testCases = [
5+
{
6+
input: 39,
7+
output: 3,
8+
},
9+
{
10+
input: 4,
11+
output: 0,
12+
},
13+
{
14+
input: 999,
15+
output: 4,
16+
},
17+
{
18+
input: 12380,
19+
output: 1,
20+
},
21+
{
22+
input: 9689313,
23+
output: 5,
24+
},
25+
{
26+
input: 78,
27+
output: 3,
28+
},
29+
{
30+
input: 6827174,
31+
output: 6,
32+
},
33+
];
34+
35+
it('should return a number type', () => {
36+
expect(typeof persistence(39)).toBe('number');
37+
});
38+
39+
it.each(testCases)('should return $output', ({ input, output }) => {
40+
expect(persistence(input)).toBe(output);
41+
});
42+
});

0 commit comments

Comments
 (0)