The number of daffodils in the for loop small exercise in JavaScript

What are daffodils? ?

Daffodil: A daffodil number is a 3-digit number whose sum to the power of 3 in each digit is equal to itself, for example: abc=a *a *a+b * b* b+c* c *c

Table of contents

The first step: the framework

The second step: analysis, first of all, we can know that the daffodil is a 3-digit number according to the title, so we can get a judgment condition that it is greater than 100 and less than 1000

Step 3: If we want to perform a type of judgment operation here, the premise is whether we want to get the hundreds, tens, and ones, so the above code

Step 4: The Ten

Step 5: Units

Step 6: Judgment

Step 7: Continue to judge

Step 8: Output

Finally: full code


 

 

The first step: the framework

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			
		</script>
	</body>
</html>

The second step: analysis, first of all, we can know that the daffodil is a 3-digit number according to the title, so we can get a judgment condition that it is greater than 100 and less than 1000

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			var count = 0;
			
			for (var i = 100; i <= 1000; i++) {
				}







			
		</script>
	</body>
</html>

Step 3: If we want to perform a type of judgment operation here, the premise is whether we want to obtain the hundreds, tens, and ones, so the above code is used to operate

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			var count = 0;
			// var num = i;
			for (var i = 100; i <= 1000; i++) {
				// 求百位
				var a = parseInt(i / 100);
				
				}
			
		
		
		</script>
	</body>
</html>

If there is a parsenint here, the function is to round up, that is, 3.5=3. Next, we will find the ten-digit method.

Step 4: The Ten

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			var count = 0;
			// var num = i;
			for (var i = 100; i <= 1000; i++) {
				// 求百位
				var a = parseInt(i / 100);
				// 求十位
				var b = parseInt((i % 100) / 10);
				
		
		</script>
	</body>
</html>

 Parsenint is also used here, and a remainder is %, for example 153%10=53, so here we get a two-digit number, and then divide by 10, we get 5.3, and then round up to 5, the following ask for a place

Step 5: Units

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			var count = 0;
			// var num = i;
			for (var i = 100; i <= 1000; i++) {
				// 求百位
				var a = parseInt(i / 100);
				// 求十位
				var b = parseInt((i % 100) / 10);
				// 求个位
				var c = parseInt(i % 10);
				
		
		</script>
	</body>
</html>

 If the one digit is the remainder of 10, for example, 153%10=3, and then another round will be added. In this way, we will obtain the one digit, tens digit, and hundreds digit, so we can make a judgment next.

Step 6: Judgment

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			var count = 0;
			// var num = i;
			for (var i = 100; i <= 1000; i++) {
				// 求百位
				var a = parseInt(i / 100);
				// 求十位
				var b = parseInt((i % 100) / 10);
				// 求个位
				var c = parseInt(i % 10);
				// Math.pow代表web里的一个数学函数,即a的3次方
				if (Math.pow(a, 3) ) {
				
		
		</script>
	</body>
</html>

 A function is used here. Math.pow represents a mathematical function in the web, that is, the third power of a. You can also write a**a

Step 7: Continue to judge

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			var count = 0;
			// var num = i;
			for (var i = 100; i <= 1000; i++) {
				// 求百位
				var a = parseInt(i / 100);
				// 求十位
				var b = parseInt((i % 100) / 10);
				// 求个位
				var c = parseInt(i % 10);
				// Math.pow代表web里的一个数学函数,即a的3次方
				if (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3) == i) {
					
		
		</script>
	</body>
</html>

Step 8: Output

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			var count = 0;
			// var num = i;
			for (var i = 100; i <= 1000; i++) {
				// 求百位
				var a = parseInt(i / 100);
				// 求十位
				var b = parseInt((i % 100) / 10);
				// 求个位
				var c = parseInt(i % 10);
				// Math.pow代表web里的一个数学函数,即a的3次方
				if (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3) == i) {
					count++
					console.log(i);

				}
			}
			console.log(count);
		
		</script>
	</body>
</html>

Finally: full code

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			var count = 0;
			// var num = i;
			for (var i = 100; i <= 1000; i++) {
				// 求百位
				var a = parseInt(i / 100);
				// 求十位
				var b = parseInt((i % 100) / 10);
				// 求个位
				var c = parseInt(i % 10);
				// Math.pow代表web里的一个数学函数,即a的3次方
				if (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3) == i) {
					count++
					console.log(i);

				}
			}
			console.log(count);
		
		</script>
	</body>











</html>














<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			var count = 0;
			// var num = i;
			for (var i = 100; i <= 1000; i++) {
				// 求百位
				var a = parseInt(i / 100);
				// 求十位
				var b = parseInt((i % 100) / 10);
				// 求个位
				var c = parseInt(i % 10);
				// Math.pow代表web里的一个数学函数,即a的3次方
				if (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3) == i) {
					count++
					console.log(i);

				}
			}
			console.log(count);







		</script>
	</body>
</html>

 

Guess you like

Origin blog.csdn.net/tea_tea_/article/details/126272489