Ad

The following is a function that takes a single number (digit) and then turns it in to a string (returned eg. 'one').

Refactor this code to be smaller and/or faster. It currently takes 700ms-800ms. You can use switch statements, inline ifs or anything else that you think could work (hint: arrays).

Happy coding!

function digitToText(digit) {
  if (digit == 1) {
    return 'one'
  }
  if (digit == 2) {
    return 'two'
  }
  if (digit == 3) {
    return 'three'
  }
  if (digit == 4) {
    return 'four'
  }
  if (digit == 5) {
    return 'five'
  }
  if (digit == 6) {
    return 'six'
  }
  if (digit == 7) {
    return 'seven'
  }
  if (digit == 8) {
    return 'eight'
  }
  if (digit == 9) {
    return 'nine'
  }
  if (digit == 0) {
    return 'zero'
  }
}