-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpandedForm.js
More file actions
39 lines (26 loc) · 1.11 KB
/
expandedForm.js
File metadata and controls
39 lines (26 loc) · 1.11 KB
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
// Write Number in Expanded Form
// You will be given a number and you will need to return it as a string in Expanded Form. For example:
// expandedForm(12); // Should return '10 + 2'
// expandedForm(42); // Should return '40 + 2'
// expandedForm(70304); // Should return '70000 + 300 + 4'
// NOTE: All numbers will be whole numbers greater than 0.
// If you liked this kata, check out part 2!!
function expandedForm(num) {
const size = num.toString().length
let subtractNumber = "1"
for(let i = 1; i <= size; i++) {
subtractNumber = subtractNumber.concat("0")
}
let minifiedNumber = num / subtractNumber
minifiedNumber = minifiedNumber.toString().split("").filter(n => n !== ".").reverse()
minifiedNumber = minifiedNumber.map((m, size) => {
multiplyNumber = "1"
for(let i = 0; i < size; i++) {
multiplyNumber = multiplyNumber.concat("0")
}
return m * multiplyNumber
})
minifiedNumber = minifiedNumber.filter(v => v != 0).reverse()
return minifiedNumber.join(" + ")
}
console.log(expandedForm(70304))