0

Recently I made a function called Converter. It was working perfectly in the morning and now when I am using it, the code is not being able to run when I type numeric like 3 it should print like Three but I do now know what is the problem and problem also not showing. I am new in Javascript Here is the code

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script>
            function abc()
            {
             //   alert('hello');
                var amt=parseInt(document.getElementById('t1'));
                var d="";
                var ones=Array("","One","Two","Three","four","five");
                var tens=Array("","","Twenty","Thirthy","Fourthy","fifthy");
                var hundreds=Array("","One hundred","Two hundred","Three hundred","Four hundred","Five hundred");
                if(amt>=1&&amt<=19)
                {
                    d=ones[amt];

                }
                  document.getElementById('p1').innerHTML=d;
               // document.write(d);

            }
            </script>
    </head>
    <body>

                <input type="text" id="t1" />

                <input type="button"  value="Convert" onclick="abc()" />

                <p id="p1"></p>

    </body>
</html>
Learner
  • 5
  • 5

3 Answers3

0

You forgot to parse the value of textbox it should be

document.getElementById('t1').value

0

In addition to my comment on not grabbing the value.

Use let amt = document.getElementById('t1').value. This returns the value inside the input.

Someone made a good example for what you are trying to do here: Convert digits into words with JavaScript

var a = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ', 'ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen '];
var b = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];

function inWords(num) {
  if ((num = num.toString()).length > 9) return 'overflow';
  n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
  if (!n) return;
  var str = '';
  str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
  str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : '';
  str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
  str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
  str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) + 'only ' : '';
  return str;
}

document.getElementById('number').onkeyup = function() {
  document.getElementById('words').innerHTML = inWords(document.getElementById('number').value);
};
<span id="words"></span>
<input id="number" type="text" />
ABC
  • 2,068
  • 1
  • 10
  • 21
-1

Your bug was caused of this line: parseInt(document.getElementById('t1')); You were trying to parseInt(html input element). There is no way to parse HTML value to int, so it was returning NaN, which cannot be converted to "Three" or any other number. Here's working code:

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script>
            function abc()
            {
              
                var amt=document.getElementById('t1').value;
                var d="";
                var ones=Array("","One","Two","Three","four","five");
                var tens=Array("","","Twenty","Thirthy","Fourthy","fifthy");
                var hundreds=Array("","One hundred","Two hundred","Three hundred","Four hundred","Five hundred");
                if(amt>=1&&amt<=19)
                {
                    d=ones[amt];
                }
                  document.getElementById('p1').innerHTML=d;
               // document.write(d);

            }
            </script>
    </head>
    <body>

                <input type="text" id="t1" />

                <input type="button"  value="Convert" onclick="abc()" />

                <p id="p1"></p>

    </body>
</html>
Dolidod Teethtard
  • 553
  • 1
  • 7
  • 22