JavaScript Loop Samples

Print numbers from 1 to 10 on the screen.

for (i = 1; i <= 10; i++) {
            document.write(i+"<br>")
        }

print 10 times in the bottom.

for (i = 1; i <= 10; i++) {
            document.write("I love codeblogger"+"<br>")
        }

List the ones with numbers from 1-50 on the screen.

for (i = 1; i <= 10; i++) {
            if (i % 2 == 0) {
                document.write(i+"<br>")
            }
        }

Listing numbers from 1 to 100 that are fully divided into 3 and 5.

 for (i = 1; i <= 100; i++) {
            if (i % 3 == 0 && i % 5 == 0) {
                document.write(i+"<br>")
            }
        }

Print the user-entered text as many times as the user enters.

var text = window.prompt("text:");
        var piece = window.prompt("piece:")
        for (var i = 0; i <= piece; i++) {
            document.write(text+"<br>")
        }

Print on the screen by magnifying the text that the user entered.

var text = window.prompt("text:");
        for (var i = 6; i > 0; i--) {
            document.write("<h"+i+">"+text+"</h"+i+">");
        }

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started