0
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.

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
KIJIRI
  • 101
  • If I understand correctly you are looking for a digit to word string conversion? So "1" => "one", "2" => "two" ? If so, then your question is a duplicate of [this question](https://stackoverflow.com/questions/14766951/convert-digits-into-words-with-javascript). – Sander Jul 19 '20 at 04:57

3 Answers3

1

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])
charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

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"))
Sven.hig
  • 4,449
  • 2
  • 8
  • 18
0

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>
Raxel21
  • 407
  • 2
  • 9