-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoZerosForHeros.js
More file actions
44 lines (40 loc) · 800 Bytes
/
Copy pathNoZerosForHeros.js
File metadata and controls
44 lines (40 loc) · 800 Bytes
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
40
41
42
43
44
/**
*
* Description:
*
* Numbers ending with zeros are boring.
*
* They might be fun in your world, but not here.
*
* Get rid of them. Only the ending ones.
*
* 1450 -> 145
* 960000 -> 96
* 1050 -> 105
* -1050 -> -105
*
* Zero alone is fine, don't worry about it. Poor guy anyway
*
* Kata URL: https://www.codewars.com/kata/570a6a46455d08ff8d001002
*
* @param {*} n
*/
function noBoringZeros(n) {
let numArray = Array.from(String(n), Number);
let counter = 0;
let result = "";
if (n === 0) {
return 0;
}
for (let i = numArray.length - 1; i >= 0; i--) {
if (numArray[i] !== 0) {
counter = i;
break;
}
}
for (let i = 0; i <= counter; i++) {
result += numArray[i];
}
return result;
}
console.log(noBoringZeros(9500200021510000));