The following is a function that takes a single number (digit) and then turns it in to a string (returned eg. 'one').
Refactor this code to be smaller and/or faster. It currently takes 700ms-800ms. You can use switch statements, inline ifs or anything else that you think could work (hint: arrays).
Happy coding!
function digitToText(digit) {
if (digit == 1) {
return 'one'
}
if (digit == 2) {
return 'two'
}
if (digit == 3) {
return 'three'
}
if (digit == 4) {
return 'four'
}
if (digit == 5) {
return 'five'
}
if (digit == 6) {
return 'six'
}
if (digit == 7) {
return 'seven'
}
if (digit == 8) {
return 'eight'
}
if (digit == 9) {
return 'nine'
}
if (digit == 0) {
return 'zero'
}
}
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
describe("Solution", function() {
it("should test for something", function() {
// Test.assertEquals(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
assert.strictEqual(digitToText(1), 'one')
assert.strictEqual(digitToText(2), 'two')
assert.strictEqual(digitToText(3), 'three')
assert.strictEqual(digitToText(4), 'four')
assert.strictEqual(digitToText(5), 'five')
assert.strictEqual(digitToText(6), 'six')
assert.strictEqual(digitToText(7), 'seven')
assert.strictEqual(digitToText(8), 'eight')
assert.strictEqual(digitToText(9), 'nine')
assert.strictEqual(digitToText(0), 'zero')
assert.strictEqual(digitToText(11), undefined)
});
});