JSFiddle - React, Tailwind, and code Playground

by Hifni Nazeer

HTML

<label>Price</label>
<input type="text" id="inputPrice" value=16 />
<br/>
<label>Qty</label>
<input type="text" id="inputQty" value=1 />
<br/>
<label>Amount</label>
<input type="text" id="outputAmount" value=0 />
<label id="lblAmount"></label>

JavaScript

$(document).ready(function () {
  var v = 0.0

  v = calculateAmount($("#inputQty").val(), $("#inputPrice").val())
  $("#outputAmount").val(v)

  $("#inputQty").change(function (e) {
    console.log("On Change")
    $("#outputAmount").val(
      calculateAmount($(this).val(), $("#inputPrice").val()),
    )
  })

  $("#inputQty").keyup(function () {
    console.log("On KeyUp")
    $("#outputAmount").val(
      calculateAmount($(this).val(), $("#inputPrice").val()),
    )
  })
})

function calculateAmount(qty, price) {
  var inQty = 0
  var inPrice = 0.0
  var outAmount = 0.0

  if ($.isNumeric(qty)) {
    inQty = parseInt(qty)
  }

  if ($.isNumeric(price)) {
    inPrice = parseFloat(price)
  }

  outAmount = inPrice * inQty
  console.log(outAmount)
  console.log(numberToEnglish(outAmount))

  return outAmount.toFixed(2)
}

/**
 * Convert an integer to its words representation
 *
 * @author McShaman (http://stackoverflow.com/users/788657/mcshaman)
 * @source http://stackoverflow.com/questions/14766951/convert-digits-into-words-with-javascript
 */
function numberToEnglish(n, custom_join_character) {
  var string = n.toString(),
    units,
    tens,
    scales,
    start,
    end,
    chunks,
    chunksLen,
    chunk,
    ints,
    i,
    word,
    words

  var and = custom_join_character || "and"

  /* Is number zero? */
  if (parseInt(string) === 0) {
    return "zero"
  }

  /* Array of units as words */
  units = [
    "",
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine",
    "ten",
    "eleven",
    "twelve",
    "thirteen",
    "fourteen",
    "fifteen",
    "sixteen",
    "seventeen",
    "eighteen",
    "nineteen",
  ]

  /* Array of tens as words */
  tens = [
    "",
    "",
    "twenty",
    "thirty",
    "forty",
    "fifty",
    "sixty",
    "seventy",
    "eighty",
    "ninety",
  ]

  /* Array of scales as words */
  scales = [
    "",
    "thousand",
    "million",
    "billion",
   ...