Detailed explanation of loop usage in javascript examples

Detailed explanation of loop usage in javascript examples

I was bored and sorted out some simple exercises about loops. I hope it will be helpful to those who are just starting to learn JS.

1. Print the number and sum of all multiples of 7 between 1 and 100

var sum=0;
var n=0;
for(var i=1;i<=100;i++){
	if(i%7==0){
		sum+=i;
		++n;
		}
	}
console.log("The number is: "+n+", the sum is: "+sum);

Running results:

2. Assuming the annual interest rate of investment is 5%, how many years will it take to increase from 1,000 to 5,000?

var money=1000;
var i=0;
while(money<=5000){
	money=money*1.05;
	++i;
}
console.log("A total of "+i+" years is required");

Running results:

3. Find the daffodil number between 100 and 1000. A daffodil number is a 3-digit number where the sum of the 3 powers of each digit is equal to itself. For example, 1^3 + 5^3 + 3^3 = 153

for(var i=100;i<1000;i++){
	//Get the number in the hundreds place var a=parseInt(i/100);
	//Get the number in the tens place var b=parseInt(i/10%10);
	// Extract the number in the ones place var c=parseInt(i%10);
	 if(a*a*a+b*b*b+c*c*c==i){
	    console.log(i);
		}
}

The tens digit can also be obtained using var b=paraseInt((i-bai*100)/10).

Running results:

4. Print the sum of all odd numbers between 1-100. All numbers that are not divisible by 2 are odd numbers.

var sum=0;
for(var i=1;i<=100;i++){
	if(i%2==0){
		sum+=i;
	}
}
console.log("The sum of odd numbers is: "+sum);

Running results:

5. Determine the prime numbers between 2-100. A prime number is a natural number greater than 1 that cannot be divided by any other natural numbers except 1 and itself.

for(var i=2;i<=100;i++){
	var flag = true;
	//If the loop reaches i, there will be repeated factors for(var j=2;j<=Math.sqrt(i);j++){
		if(i%j==0){
			flag=false;
			break;
		}
	}
	if(flag==true){
		document.write(i+"&nbsp");
	}
}

Running results:

6. Printing triangle

From little to more

for(var i=1;i<=5;i++){
	for(var j=1;j<=i;j++){
		document.write("*");
	}
	document.write("<br/>");
} 

From more to less

for(var i=0;i<5;i++){
	for(var j=0;j<5-i;j++){
		document.write("*");
	}
	document.write("<br/>");
} 

7. Print the multiplication table

for(var i=1;i<=9;i++){
	for(var j=1;j<=i;j++){
		document.write(i+"*"+j+"="+i*j+"&nbsp;&nbsp;")
	}
	document.write("<br/>");//line break}

Running results:

This is the end of this article about javascript examples explaining the use of loops. For more relevant javascript loop content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Do you know all 24 methods of JavaScript loop traversal?
  • Detailed discussion of the differences between loops in JavaScript
  • JavaScript event loop case study
  • Summary of the use of three for loop statements in JavaScript (for, for...in, for...of)
  • Analysis of the event loop mechanism of js
  • How many common loops do you know about array traversal in JS?
  • Detailed explanation of various loop speed tests in JS that you don’t know
  • JavaScript implements circular carousel

<<:  Docker implements container port binding local port

>>:  IE8 provides a good experience: Activities

Recommend

Docker builds jenkins+maven code building and deployment platform

Table of contents Docker Basic Concepts Docker in...

Detailed explanation of location and rewrite usage in nginx

1. Summary of location usage Location can locate ...

Summary of using the exclamation mark command (!) in Linux

Preface Recently, our company has configured mbp,...

100-1% of the content on the website is navigation

Website, (100-1)% of the content is navigation 1....

How to quickly modify the root password under CentOS8

Start the centos8 virtual machine and press the u...

Analysis and solution of a MySQL slow log monitoring false alarm problem

Previously, for various reasons, some alarms were...

How to deploy MySQL and Redis services using Docker

Table of contents How to deploy MySQL service usi...

How to configure jdk environment under Linux

1. Go to the official website to download the jdk...

CentOS8 network card configuration file

1. Introduction CentOS8 system update, the new ve...

Tutorial on installing Android Studio on Ubuntu 19 and below

Based on past experience, taking notes after comp...

MySQL learning database search statement DQL Xiaobai chapter

Table of contents 1. Simple retrieval of data 2. ...

Scoring rules of YSlow, a webpage scoring plugin developed by Yahoo

YSlow is a page scoring plug-in developed by Yaho...

In-depth analysis of HTML table tags and related line break issues

What is a table? Table is an Html table, a carrie...

Three implementation methods of Mysql copy table and grant analysis

How to quickly copy a table First, create a table...