Skip to content

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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: