Javascript script to print n elements of Fizz Buzz sequence.

// © 2017 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net

<html>
<body>
    Enter a number:  <input type="text" id="data">
    <button onclick="show()">Convert</button>
    <p id="newText"></p>

    <script>
        function show(){
            var data = document.getElementById("data").value;
            var n = parseInt(data);

            for (var i = 1; i <= n; i++)
            {
                if (i % 3 == 0 && i % 5 == 0)
                {
                    document.getElementById("newText").innerHTML += "Fizz Buzz";
                }
                else if (i % 3 == 0)
                {
                    document.getElementById("newText").innerHTML += "Fizz";
                }
                else if (i % 5 == 0)
                {
                    document.getElementById("newText").innerHTML += "Buzz";
                }
                else
                {
                    document.getElementById("newText").innerHTML += i;
                }

                document.getElementById("newText").innerHTML += " ";
            } 
        }
    </script>
</body>
</html>

 

Javascript Fizz Buzz
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *