0

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>

Edited by chijkl: n/a

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by chijkl
0

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,

0

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 :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.