skip to Main Content
function convertAmountToWords(amount) {
  const words = [
    "Zero",
    "One",
    "Two",
    "Three",
    "Four",
    "Five",
    "Six",
    "Seven",
    "Eight",
    "Nine",
    "Ten",
    "Eleven",
    "Twelve",
    "Thirteen",
    "Fourteen",
    "Fifteen",
    "Sixteen",
    "Seventeen",
    "Eighteen",
    "Nineteen",
  ];

  const tensWords = [
    "",
    "",
    "Twenty",
    "Thirty",
    "Forty",
    "Fifty",
    "Sixty",
    "Seventy",
    "Eighty",
    "Ninety",
  ];

  const centsWords = [
    "",
    "One Cent",
    "Two Cents",
    "Three Cents",
    "Four Cents",
    "Five Cents",
    "Six Cents",
    "Seven Cents",
    "Eight Cents",
    "Nine Cents",
  ];

  const [dollars, cents] = amount.slice(1).split(".");
  const dollarWords =
    dollars === "0" ?
    "Zero" :
    dollars < 20 ?
    words[Number(dollars)] :
    tensWords[Math.floor(dollars / 10)] +
    (dollars % 10 === 0 ? "" : ` ${words[dollars % 10]}`);
  const centWords = cents ? centsWords[Number(cents)] : "";

  return `${dollarWords} Dollar and ${centWords}`;
}

const amountInWords = convertAmountToWords("$10.20");
console.log(amountInWords); // Output: "Ten Dollar and Twenty Cents"

In this code example that (.20) is undefined, I want to above example is ($10.20),
I want "Ten Dollar and Twenty Cents".
How to recode above example.
Help me please.

2

Answers


  1. I fixed the logic for handling the cents. By using the tensWords and words arrays, I converted the cents into words. The code checks if cents exist and are not zero before including the cents part in the final result. This ensures that both the dollar and cent parts are correctly converted and displayed in the desired format.

    function convertAmountToWords(amount) {
      const words = [
        "Zero",
        "One",
        "Two",
        "Three",
        "Four",
        "Five",
        "Six",
        "Seven",
        "Eight",
        "Nine",
        "Ten",
        "Eleven",
        "Twelve",
        "Thirteen",
        "Fourteen",
        "Fifteen",
        "Sixteen",
        "Seventeen",
        "Eighteen",
        "Nineteen",
      ];
    
      const tensWords = [
        "",
        "",
        "Twenty",
        "Thirty",
        "Forty",
        "Fifty",
        "Sixty",
        "Seventy",
        "Eighty",
        "Ninety",
      ];
    
      const centsWords = [
        "Zero Cents",
        "One Cent",
        "Two Cents",
        "Three Cents",
        "Four Cents",
        "Five Cents",
        "Six Cents",
        "Seven Cents",
        "Eight Cents",
        "Nine Cents",
      ];
    
      const [dollars, cents] = amount.slice(1).split(".");
      const dollarWords =
        dollars === "0"
          ? "Zero"
          : dollars < 20
          ? words[Number(dollars)]
          : tensWords[Math.floor(dollars / 10)] +
            (dollars % 10 === 0 ? "" : ` ${words[dollars % 10]}`);
      const centWords =
        cents && Number(cents) !== 0 ? tensWords[Math.floor(cents / 10)] + (cents % 10 === 0 ? "" : ` ${words[cents % 10]}`) : "";
    
      const dollarPart = `${dollarWords} Dollar`;
      const centPart = centWords ? ` and ${centWords} Cents` : "";
    
      return `${dollarPart}${centPart}`;
    }
    
    const amountInWords = convertAmountToWords("$10.90");
    console.log(amountInWords); // Output: "Ten Dollar and Twenty Cents"
    Login or Signup to reply.
  2. It’s because your centsWords array only goes up to nine. So instead of creating a new array, it’s easier and better to use the same array and the same method you used to generate the dollarWords.

    function convertAmountToWords(amount) {
      const words = [
        "Zero",
        "One",
        "Two",
        "Three",
        "Four",
        "Five",
        "Six",
        "Seven",
        "Eight",
        "Nine",
        "Ten",
        "Eleven",
        "Twelve",
        "Thirteen",
        "Fourteen",
        "Fifteen",
        "Sixteen",
        "Seventeen",
        "Eighteen",
        "Nineteen",
      ];
    
      const tensWords = [
        "",
        "",
        "Twenty",
        "Thirty",
        "Forty",
        "Fifty",
        "Sixty",
        "Seventy",
        "Eighty",
        "Ninety",
      ];
    
      const [dollars, cents] = amount.slice(1).split(".");
      const dollarWords = (
        dollars === "0" ?
          "Zero"
        :
          dollars < 20 ?
            words[dollars]
          :
            tensWords[Math.floor(dollars / 10)] + (dollars % 10 === 0 ? "" : ` ${words[dollars % 10]}`)
      );
      const centWords = (
        cents === "0" ?
          "Zero"
        :
          cents < 20 ?
            words[cents]
          :
            tensWords[Math.floor(cents / 10)] + (cents % 10 === 0 ? "" : ` ${words[cents % 10]}`)
      );
    
      return `${dollarWords} Dollar and ${centWords} Cent`;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search