Friday, August 21, 2015

Explode digit from user input and count character of digit using javascript

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>explode</title>
</head>
<body>
<input type="text" name="" id="given_value"><br><br>
<button onclick="explod()">Click to explode the digit</button>
<br><br>
Result:
<br>
<div id="result"></div>
<script>
    function explod(){
        var given_value = parseInt(document.getElementById("given_value").value);
        var explode;
        var count = 0;
        if(isNaN(given_value)){
            explode = "You have to enter a positive Number";
        }else {
            given_value = String(given_value);
            for ( var i = 0; i < given_value.length; i++ )
            {
                if(given_value.charAt(i) == "-"){
                    explode = "You have to enter a positive Number";
                    break;
                } else if(explode == undefined){
                    explode = given_value.charAt(i);
                }else{
                    explode = explode +"<br>"+ given_value.charAt(i) ;
                    count++;
                }
            }

        }
        if(count >= 0){
            count = count + 1;
            document.getElementById("result").innerHTML = explode + "<br>Number of digit you've entered: " + count;
        }else{
            document.getElementById("result").innerHTML = explode;
        }
    }
</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; }