Saturday, August 22, 2015

How to looping star - increment and decrement from given value

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>star looping</title>
</head>
<body>
<input type="text" name="" id="number">
<button onclick="star_looping()">Click to loop</button>
<br>
<div id="result"></div>

<script>
function star_looping(){
    var number = parseInt(document.getElementById('number').value);
    var star = "";
    for (var i = 0; i <= number; i++){
        for (var j = 0; j < i; j++){
           star = star + "* ";
        }
        star = star + "<br>";
    }
    for(var i = number-1; i > 0; i--){
        for (var j = 0; j < i; j++){
            star = star + "* ";
        }
        star = star + "<br>";
    }
    document.getElementById('result').innerHTML = star;
}
</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; }