-
Notifications
You must be signed in to change notification settings - Fork 1
/
expandedForm.js
35 lines (26 loc) · 1.46 KB
/
expandedForm.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
// Write Number in Expanded Form from https://www.codewars.com/kata/write-number-in-expanded-form/train/javascript
/*Write Number in Expanded Form
You will be given a number and you will need to return it as a string in Expanded Form (https://www.mathplacementreview.com/arithmetic/whole-numbers.php#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 (https://www.codewars.com/kata/write-number-in-expanded-form-part-2)!!*/
// const expandedForm = n => [...String(n)].map((d, i, n) => d.padEnd(n.length - i, 0)).filter(Number).join(' + ');
// codewars problem does not support ES2017 features, refactor to exclude padEnd method:
const expandedForm = n => [...`${n}`].map((d, i, n) => d + '0'.repeat(n.length - i - 1)).filter(Number).join(' + ');
const expect = require('chai').expect;
describe('expandedForm function', () => {
it('should return a string', () => {
expect(expandedForm(2)).to.be.a('string');
});
it('should return \'10 + 2\' given 12', () => {
expect(expandedForm(12)).to.equal('10 + 2');
});
it('should return \'40 + 2\' given 42', () => {
expect(expandedForm(42)).to.equal('40 + 2');
});
it('should return \'70000 + 300 + 4\' given 70304', () => {
expect(expandedForm(70304)).to.equal('70000 + 300 + 4');
});
});