I have problem with the convert button please help
Thanks in advance

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script type="text/javascript">
var th = ['',];
var dg = ['zero', 'ichi', 'ni', 'san', 'yon', 'go', 'roku', 'nana', 'hachi', 'kyuu'];
var tn = ['juu', 'juuichi', 'juuni', 'juusan', 'juushi', 'juugo', 'juuruko', 'juushichi', 'juuhachi', 'juuku'];
var tw = ['nijuu', 'sanjuu', 'yonjuu', 'gojuu', 'rokujuu', 'nanajuu', 'hachijuu', 'kyuujuu'];

function update(){
    var numString =   document.getElementById('number').value;
    if (numString == '0') {
        document.getElementById('container').innerHTML = 'Zero';
        return;
    }
    if (numString == 0) {
        document.getElementById('container').innerHTML = '<h1>Please enter a number:)</h1>';
        return;
    }

    var output = toWords(numString);
    //print the output
    document.getElementById('container').innerHTML = output;
}

function toWords(s) {
    s = s.toString();
    s = s.replace(/[\, ]/g, '');
    if (s != parseFloat(s)) return '<h1>not a number</h2>';
    var x = s.indexOf('.');
    if (x == -1) x = s.length;
    if (x > 3) return 'too big';
    var n = s.split('');
    var str = '';
    var sk = 0;
    for (var i = 0; i < x; i++) {
        if ((x - i) % 3 == 2) {
            if (n[i] == '1') {
                str += tn[Number(n[i + 1])] + ' ';
                i++;
                sk = 1;
            } else if (n[i] != 0) {
                str += tw[n[i] - 2] + '';
                sk = 1;
            }
        } else if (n[i] != 0) {
            str += dg[n[i]] + ' ';
            if ((x - i) % 3 == 0) str += 'hyako ';
            sk = 1;
        }
        if ((x - i) % 3 == 1) {
            if (sk) str += th[(x - i - 1) / 3] + ' ';
            sk = 0;
        }
    }
    if (x != s.length) {
        var y = s.length;
        str += 'point ';
        for (var i = x + 1; i < y; i++) str += dg[n[i]] + ' ';
    }
    return str.replace(/\s+/g, ' ');
}
</script>

</head>
<body bgcolor="#FAF8CC"><center>
<h1>integer to word</h1>
<hr width="200"/>
<input type="text"
    id="number"
    size="70"
    />
    <input type="button" onclick="update()">
<input type="text" name="translated text here">
                <br/>
<br/>

</center>
</body>
</html>

Hi,

What is the error that you are getting with button? Let us know.


Thank you,

Hi,

You have missed few things in the code. I have modified the code and it is working fine. Here is the updated code;

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script type="text/javascript">
var th = ['',];
var dg = ['zero', 'ichi', 'ni', 'san', 'yon', 'go', 'roku', 'nana', 'hachi', 'kyuu'];
var tn = ['juu', 'juuichi', 'juuni', 'juusan', 'juushi', 'juugo', 'juuruko', 'juushichi', 'juuhachi', 'juuku'];
var tw = ['nijuu', 'sanjuu', 'yonjuu', 'gojuu', 'rokujuu', 'nanajuu', 'hachijuu', 'kyuujuu'];
 
function update(){
    var numString =   document.getElementById('number').value;
    if (numString == '0') {
        document.getElementById('container').value = 'Zero';
        return;
    }
    if (numString == 0) {
        document.getElementById('container').value = '<h1>Please enter a number:)</h1>';
        return;
    }
 
    var output = toWords(numString);
    //print the output
    document.getElementById('container').value = output;
}
 
function toWords(s) {
    s = s.toString();
    s = s.replace(/[\, ]/g, '');
    if (s != parseFloat(s)) return '<h1>not a number</h2>';
    var x = s.indexOf('.');
    if (x == -1) x = s.length;
    if (x > 3) return 'too big';
    var n = s.split('');
    var str = '';
    var sk = 0;
    for (var i = 0; i < x; i++) {
        if ((x - i) % 3 == 2) {
            if (n[i] == '1') {
                str += tn[Number(n[i + 1])] + ' ';
                i++;
                sk = 1;
            } else if (n[i] != 0) {
                str += tw[n[i] - 2] + '';
                sk = 1;
            }
        } else if (n[i] != 0) {
            str += dg[n[i]] + ' ';
            if ((x - i) % 3 == 0) str += 'hyako ';
            sk = 1;
        }
        if ((x - i) % 3 == 1) {
            if (sk) str += th[(x - i - 1) / 3] + ' ';
            sk = 0;
        }
    }
    if (x != s.length) {
        var y = s.length;
        str += 'point ';
        for (var i = x + 1; i < y; i++) str += dg[n[i]] + ' ';
    }
    return str.replace(/\s+/g, ' ');
}
</script>
 
</head>
<body bgcolor="#FAF8CC"><center>
<h1>integer to word</h1>
<hr width="200"/>
<input type="text"
    id="number"
    size="70"
    />
    <input id="submit" type="button" value="submit" onclick="update()">
<input type="text" id="container" name="translated text here">
                <br/>
<br/>
 
</center>
</body>
</html>

Changes are in Red color.

Thank you,

Hi,

You have missed few things in the code. I have modified the code and it is working fine. Here is the updated code;

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script type="text/javascript">
var th = ['',];
var dg = ['zero', 'ichi', 'ni', 'san', 'yon', 'go', 'roku', 'nana', 'hachi', 'kyuu'];
var tn = ['juu', 'juuichi', 'juuni', 'juusan', 'juushi', 'juugo', 'juuruko', 'juushichi', 'juuhachi', 'juuku'];
var tw = ['nijuu', 'sanjuu', 'yonjuu', 'gojuu', 'rokujuu', 'nanajuu', 'hachijuu', 'kyuujuu'];
 
function update(){
    var numString =   document.getElementById('number').value;
    if (numString == '0') {
        document.getElementById('container').value = 'Zero';
        return;
    }
    if (numString == 0) {
        document.getElementById('container').value = '<h1>Please enter a number:)</h1>';
        return;
    }
 
    var output = toWords(numString);
    //print the output
    document.getElementById('container').value = output;
}
 
function toWords(s) {
    s = s.toString();
    s = s.replace(/[\, ]/g, '');
    if (s != parseFloat(s)) return '<h1>not a number</h2>';
    var x = s.indexOf('.');
    if (x == -1) x = s.length;
    if (x > 3) return 'too big';
    var n = s.split('');
    var str = '';
    var sk = 0;
    for (var i = 0; i < x; i++) {
        if ((x - i) % 3 == 2) {
            if (n[i] == '1') {
                str += tn[Number(n[i + 1])] + ' ';
                i++;
                sk = 1;
            } else if (n[i] != 0) {
                str += tw[n[i] - 2] + '';
                sk = 1;
            }
        } else if (n[i] != 0) {
            str += dg[n[i]] + ' ';
            if ((x - i) % 3 == 0) str += 'hyako ';
            sk = 1;
        }
        if ((x - i) % 3 == 1) {
            if (sk) str += th[(x - i - 1) / 3] + ' ';
            sk = 0;
        }
    }
    if (x != s.length) {
        var y = s.length;
        str += 'point ';
        for (var i = x + 1; i < y; i++) str += dg[n[i]] + ' ';
    }
    return str.replace(/\s+/g, ' ');
}
</script>
 
</head>
<body bgcolor="#FAF8CC"><center>
<h1>integer to word</h1>
<hr width="200"/>
<input type="text"
    id="number"
    size="70"
    />
    <input id="submit" type="button" value="submit" onclick="update()">
<input type="text" id="container" name="translated text here">
                <br/>
<br/>
 
</center>
</body>
</html>

Changes are in Red color.

Thank you,

Thankyou somuch :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.