convert 1 to one in javascript

There are different ways to convert a number to its corresponding string in JavaScript. One simple approach is to use a mapping object to match each digit with its word representation. Here's an example function:

index.tsx
function numberToWord(num) {
  const mapping = {
    0: 'zero',
    1: 'one',
    2: 'two',
    3: 'three',
    4: 'four',
    5: 'five',
    6: 'six',
    7: 'seven',
    8: 'eight',
    9: 'nine'
  };
  const digits = String(num).split('');
  const words = digits.map(digit => mapping[digit]);
  return words.join('');
}

// example usage:
console.log(numberToWord(123)); // "onetwothree"
console.log(numberToWord(456)); // "fourfivesix"
440 chars
22 lines

This function converts the input number num to a string by splitting its digits, mapping each digit to its word representation using the mapping object, and then returning the joined words. Note that this approach only works for non-negative integers.

gistlibby LogSnag