-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7kyu-digitsExplosion.js
39 lines (30 loc) · 960 Bytes
/
7kyu-digitsExplosion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
Given a string made of digits [0-9], return a string where each digit is repeated a number of times equals to its value.
Examples
explode("312")
should return :
"333122"
explode("102269")
should return :
"12222666666999999999"
*/
//P: one input, a string of digits 0-9
//R: return a string where each digit is repeated a number of time equal to its value
//E: '312' => '333122'
// '0' => ''
// '123' => '122333'
//P: split the string and convert to an array of individual nums
// remove any zeros, since they won't be added to final string
// iterate through non-zero array, adding the num to a results string as many times as its value
// return results string
function explode(s) {
let nums = s.split('').filter(num => num!==0);
let result = '';
for(let i = 0; i < nums.length; i++){
let value = nums[i];
for(let j = 1; j<=value; j++){
result += `${value}`;
}
}
return result;
}