function digitizeNumber(number) { let digits = []; let n = +number || 0; while (n != 0) { digits.push(Math.round(n % 10)); n = Math.round(n / 10); } digits.reverse(); return digits }
- function digitizeNumber(number) {
let digits = [];let stringNumber = number && number.toString() || "";for (let i = 0; i < stringNumber.length; i += 1) {digits.push(+stringNumber.charAt(i));}return digits;- let digits = [];
- let n = +number || 0;
- while (n != 0) {
- digits.push(Math.round(n % 10));
- n = Math.round(n / 10);
- }
- digits.reverse();
- return digits
- }
// TODO: Replace examples and use TDD development by writing your own tests // These are some CW specific test methods available: // Test.expect(boolean, [optional] message) // Test.assertEquals(actual, expected, [optional] message) // Test.assertSimilar(actual, expected, [optional] message) // Test.assertNotEquals(actual, expected, [optional] message) // NodeJS assert is also automatically required for you. // assert(true) // assert.strictEqual({a: 1}, {a: 1}) // assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]}) // You can also use Chai (http://chaijs.com/) by requiring it yourself // var expect = require("chai").expect; // var assert = require("chai").assert; // require("chai").should(); describe("Solution", function(){ it("should test for something", function(){ Test.expect(digitizeNumber(123456), [1,2,3,4,5,6]); Test.expect(digitizeNumber(0), []); Test.expect(digitizeNumber(undefined), []); Test.expect(digitizeNumber(90124), [9,0,1,2,4]); Test.expect(digitizeNumber(123456), [1, 2, 3, 4, 5, 6]); Test.expect(digitizeNumber(-123), [NaN, 1, 2, 3]); //I have no idea why does this test pass }); });
- // TODO: Replace examples and use TDD development by writing your own tests
- // These are some CW specific test methods available:
- // Test.expect(boolean, [optional] message)
- // Test.assertEquals(actual, expected, [optional] message)
- // Test.assertSimilar(actual, expected, [optional] message)
- // Test.assertNotEquals(actual, expected, [optional] message)
- // NodeJS assert is also automatically required for you.
- // assert(true)
- // assert.strictEqual({a: 1}, {a: 1})
- // assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]})
- // You can also use Chai (http://chaijs.com/) by requiring it yourself
- // var expect = require("chai").expect;
- // var assert = require("chai").assert;
- // require("chai").should();
- describe("Solution", function(){
- it("should test for something", function(){
- Test.expect(digitizeNumber(123456), [1,2,3,4,5,6]);
- Test.expect(digitizeNumber(0), []);
- Test.expect(digitizeNumber(undefined), []);
- Test.expect(digitizeNumber(90124), [9,0,1,2,4]);
- Test.expect(digitizeNumber(123456), [1, 2, 3, 4, 5, 6]);
Test.expect(digitizeNumber(-123), [NaN, 1, 2, 3]);- Test.expect(digitizeNumber(-123), [NaN, 1, 2, 3]); //I have no idea why does this test pass
- });
- });