Cheque Amount in Words

Medium30 minLinkedIn

Prompt

A cheque has a box for the amount in digits ($xxx.yy) and a line where the same amount is written out in words. Write a function chequeAmount(amount) that takes the amount as a number and returns the text for that line.

chequeAmount(588.22);
// "five hundred and eighty eight dollars and twenty two cents"
chequeAmount(1);
// "one dollar"
chequeAmount(1000.5);
// "one thousand dollars and fifty cents"

Requirements

  • amount is a non-negative number with at most two decimal places
  • Words are lowercase and separated by single spaces, with no hyphens or commas
  • Put "and" between "hundred" and the rest of the number ("one hundred and five")
  • Use "dollar" and "cent" for exactly one, "dollars" and "cents" otherwise
  • Leave out the cents part when there are no cents
  • Amounts under one dollar start with "zero dollars"
  • Support amounts up to the billions