Skip to content

Instantly share code, notes, and snippets.

@ErickWendel
Created August 3, 2022 11:46
Show Gist options
  • Save ErickWendel/c53524cdf58442d0dd935968bb0a5e31 to your computer and use it in GitHub Desktop.
Save ErickWendel/c53524cdf58442d0dd935968bb0a5e31 to your computer and use it in GitHub Desktop.
// I know it's not complete. It was a TDD test I made a few years ago
//index.js
const single = {
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight',
9: 'nine',
};
const units = {
11: 'eleven',
12: 'twelve',
13: 'thrirteen',
14: 'fourteen',
15: 'fifteen',
16: 'sixteen',
17: 'seventeen',
18: 'eightteen',
19: 'nineteen',
};
// 'eleven'
// 'eleven'
// 'eleven'
// 'eleven'
// 'eleven'
// 'eleven'
const dozens = {
10: 'ten',
20: 'twenty',
30: 'thirty',
40: 'forty',
50: 'fifty',
60: 'sixty',
70: 'seventy',
80: 'eighty',
90: 'ninety',
};
const others = {
100: 'hundred',
1000: 'thousand ',
};
const numbersLessThanTen = {
0: 'zero',
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight',
9: 'nine',
};
function verifyNumberIfLessThanTen(number) {
return numbersLessThanTen[number];
}
function verifyDozens(number) {
return dozens[number];
}
function verifyUnit(number) {
return units[number];
}
function verifySingles(number) {
return single[number];
}
function getDozenText(number, value) {
// ten -> twenty {
if (number < 10) {
return verifySingles(number);
}
if (number < 20 && number > 10) {
return verifyUnit(number);
}
const newValue = value;
const prevValue = newValue[value.length - 1];
newValue[value.length - 1] = 0;
// console.log('newValue', prev);
const dozen = verifyDozens(newValue.join(''));
const rest = verifySingles(prevValue);
// console.log('rest', rest);
// console.log('prevValue', prevValue);
return `${dozen} ${rest}`;
}
function getCent(number, value) {
const first = value[0];
const firstText = `${verifySingles(first)} hundred`;
const rest = value.splice(1, value.length - 1);
const dozen = getDozenText(rest.join(''), rest);
return `${firstText} and ${dozen}`;
}
function convertNumbersToText(number) {
const value = `${number}`.split('');
if (value.length === 1) {
return verifyNumberIfLessThanTen(number);
}
// min -> 1
// max -> 9999
if (value.length === 2) {
return getDozenText(number, value);
}
if (value.length === 3) {
return getCent(number, value);
}
if (value.length === 4) {
const first = value[0];
const firstText = `${verifySingles(first)} thousand`;
const rest = value.splice(1, value.length - 1);
const cent = getCent(rest.join(''), rest);
return `${firstText} ${cent}`;
}
return '';
}
module.exports = {
convertNumbersToText,
};
//--------test.js
const { equal } = require('assert');
const { convertNumbersToText } = require('./index');
/*
Create a function in javascript to convert a number to words, e.g. 


1 returns one

10 returns ten

11 returns eleven

22 returns twenty two

111 returns one hundred and eleven

120 returns one hundred and twenty

121 returns one hundred and twenty one

300 returns three hundred



Range should be from 1 - 9999
*/
/*
one
ten
eleven
twenty
thirty
forty
fifty
sixty
seventy
eighty
ninety
hundred
thousand
100 -> undered (1, 0, 0)
20 -> twenty (2, 0)
*/
describe('numbers to text', () => {
it('should return one', () => {
const expected = 'one';
const actual = 1;
const result = convertNumbersToText(actual);
equal(result, expected);
});
it('should return eleven', () => {
const expected = 'eleven';
const actual = 11;
const result = convertNumbersToText(actual);
equal(result, expected);
});
it('should return twelve', () => {
const expected = 'twelve';
const actual = 12;
const result = convertNumbersToText(actual);
equal(result, expected);
});
it('should return twenty three', () => {
const expected = 'twenty three';
const actual = 23;
const result = convertNumbersToText(actual);
equal(result, expected);
});
it('should return forty six', () => {
const expected = 'forty six';
const actual = 46;
const result = convertNumbersToText(actual);
equal(result, expected);
});
it('should return ninety nine', () => {
const expected = 'ninety nine';
const actual = 99;
const result = convertNumbersToText(actual);
equal(result, expected);
});
it('should return one hundred and eleven', () => {
const expected = 'one hundred and eleven';
const actual = 111;
const result = convertNumbersToText(actual);
equal(result, expected);
});
it('should return one hundred twenty one ', () => {
const expected = 'one hundred and twenty one';
const actual = 121;
const result = convertNumbersToText(actual);
equal(result, expected);
});
it('should return nine hundred and twenty one ', () => {
const expected = 'nine hundred and twenty one';
const actual = 921;
const result = convertNumbersToText(actual);
equal(result, expected);
});
it('should return one thousand nine hundred and twenty one', () => {
const expected = 'one thousand nine hundred and twenty one';
const actual = 1921;
const result = convertNumbersToText(actual);
equal(result, expected);
});
// it('should return one thousand five hundred five', () => {
// const expected = 'five hundred five';
// const actual = 505;
// const result = convertNumbersToText(actual);
// equal(result, expected);
// });
it('should return nine thousand nine hundred and ninety nine', () => {
const expected = 'nine thousand nine hundred and ninety nine';
const actual = 9999;
const result = convertNumbersToText(actual);
equal(result, expected);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment