function main(englishName) {
var number = englishName;
var lastone = +number.toString().split('').pop();
console.log(lastone);
}
This is what I got so far but not sure how to get the digit to switch too a word. Such as two, one, three, etc.
function main(englishName) {
var number = englishName;
var lastone = +number.toString().split('').pop();
console.log(lastone);
}
This is what I got so far but not sure how to get the digit to switch too a word. Such as two, one, three, etc.
Use a lookup object with numbers as keys and words as values
const data = {
'1': 'one',
'2': 'two'
}
const num = 132;
const lastNum = num.toString().split('').pop()
console.log(lastNum, 'in word form is', data[lastNum])
First you have to make an object with keys as numbers in words and their corresponding values and then use split(" ")
and pop()
, to retrieve the corresponding digit simply pass it to the object
function main(numberinwords) {
const numberobj={"one":1,"two":2,"three":3,"twenty":20,"hundered":100}
var lastone = numberinwords.toString().split(" ").pop();
return numberobj[lastone]
}
console.log(main("hundred twenty two"))
const field = document.querySelector('input');
const paraph = document.querySelector('#numberInWords');
function main(number) {
// Since you want the last number and it will
// always be 0-9 you can create an array with the numbers in words
let numbers = [
'Zero',
'One',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
];
let lastNumber = number.toString().split('').pop();
// If no number is entered, numbers[+lastNumber] returns
// undefined (except for the space character)
if (numbers[+lastNumber]) {
return numbers[+lastNumber];
} else {
return 'Please enter a number';
}
}
field.addEventListener('input', function(e) {
paraph.textContent = `Number In Words: ${main(e.target.value)}`;
});
<input type="text">
<p id="numberInWords"></p>