Created
December 9, 2019 14:10
-
-
Save woliveiras/85291bd1af852c98aa70d2b49c35f79b to your computer and use it in GitHub Desktop.
Escale - Code Challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const toFrom = { | |
0: "zero", | |
1: "um", | |
2: "dois", | |
3: "três", | |
4: "quatro", | |
5: "cinco", | |
6: "seis", | |
7: "sete", | |
8: "oito", | |
9: "nove", | |
10: "dez", | |
11: "onze", | |
12: "doze", | |
13: "treze", | |
14: "quatorze", | |
15: "quinze", | |
16: "dezesseis", | |
17: "dezessete", | |
18: "dezoito", | |
19: "dezenove", | |
20: "vinte", | |
30: "trinta", | |
40: "quarenta", | |
50: "cinquenta", | |
60: "sessenta", | |
70: "setenta", | |
80: "oitenta", | |
90: "noventa", | |
100: "cem", | |
200: "duzentos", | |
300: "trezentos", | |
400: "quatrocentos", | |
500: "quinhentos", | |
600: "seiscentos", | |
700: "setecentos", | |
800: "oitocentos", | |
900: "novecentos" | |
}; | |
module.exports = toFrom; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const toFromMap = require('./constants'); | |
function numberToString(number) { | |
if(!toFromMap[number]) { | |
const string = number.toString(10).split(''); | |
let head = null; | |
let result = null; | |
if (number <= 99) { | |
head = toFromMap[string[0] + '0']; | |
result = `${head} e ${toFromMap[string[1]]}`; | |
} | |
if (string[0] === '1' && string[2] !== '0') { | |
const tail = parseInt(string[1] + string[2]); | |
result = `cento e ${numberToString(tail)}`; | |
} | |
return result; | |
} | |
return toFromMap[number]; | |
} | |
module.exports = numberToString; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const numberToString = require('./index'); | |
test('Validate 9 decimal', () => { | |
expect(numberToString(9)).toBe('nove'); | |
}); | |
test('Validate 21 decimal', () => { | |
expect(numberToString(21)).toBe('vinte e um'); | |
}); | |
test('Validate 30 decimal', () => { | |
expect(numberToString(30)).toBe('trinta'); | |
}); | |
test('Validate 99 decimal', () => { | |
expect(numberToString(99)).toBe('noventa e nove'); | |
}); | |
test('Validate 100 decimal', () => { | |
expect(numberToString(100)).toBe('cem'); | |
}); | |
test('Validate 101 decimal', () => { | |
expect(numberToString(101)).toBe('cento e um'); | |
}); | |
test('Validate 110 decimal', () => { | |
expect(numberToString(110)).toBe('cento e dez'); | |
}); | |
test('Validate 111 decimal', () => { | |
expect(numberToString(111)).toBe('cento e onze'); | |
}); | |
test('Validate 121 decimal', () => { | |
expect(numberToString(121)).toBe('cento e vinte e um'); | |
}); | |
test('Validate 201 decimal', () => { | |
expect(numberToString(201)).toBe('duzentos e um'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "desafio-escale", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"dependencies": { | |
"jest": "^24.9.0" | |
}, | |
"devDependencies": { | |
"@types/jest": "^24.0.23" | |
}, | |
"scripts": { | |
"test": "jest" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment