Saturday, August 22, 2015

Explode digit from user input and count character of digit using javascript - Math approach rather than built in function

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Explode 3</title>
</head>
<body>
<input type="text" name="" id="given_digit">
<button onclick="explode()">Click to split</button>
<div id="result"></div>

<script>
    function explode(){
        var number = document.getElementById('given_digit').value;
            number = parseInt(number);
        var remainder;
        var counter = 0;
        if (isNaN(number)){
            remainder = "You have to Enter number value";
        } else {
            while(number > 0){
                counter++;
                if(remainder == undefined){
                    remainder = number % 10;
                } else {
                    remainder = (number % 10) +"<br> "+ remainder ;
                }
                number = parseInt(number / 10);
            }

        }
            remainder = remainder + "<br> Total number of digit you've entered: <br>" + counter;
        document.getElementById('result').innerHTML = remainder;
    }
</script>
</body>
</html>

No comments:

Post a Comment

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }