The creation of arrays in Javascript and the method of finding the most value

Table of contents

Step 1: Frame

Step 2: Two ways to create an array

               1. Literal Creation

        2. Use the new keyword to create

Step 3: Find the most value (here, the way to create an array using literals) 

Step 4: Define a variable to store the most value, here we put the first element in the array and use the for loop to achieve

Step 5: Add Judgment Conditions

Step 6: Add Judgment Conditions 


Step 1: Frame

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

Step 2: Two ways to create an array

               1. Literal Creation

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

The so-called literal creation is to define a variable and then add a bracket after the equal sign, thus creating an empty array

        2. Use the new keyword to create

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			var b = new Array()
			
		</script>
	</body>
</html>

 Using new to create is to create a variable and then new, Array means an array

Step 3: Find the most value (here, the way to create an array using literals) 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			
			 var sex = [1, 2, 3, 4, 5,9,7,12,11]
			

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

Here, we first create an array, and the array elements in the array are concatenated by commas

Step 4: Define a variable to store the most value, here we put the first element in the array and use the for loop to achieve

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>		
			 var sex = [1, 2, 3, 4, 5,9,7,12,11]
			 var sum = sex[0]
			 for (var i = 0; i < sex.length; i++) {
			 
			}			
		</script>
	</body>
</html>

 Here, you will find that i is less than sex.length, length means length, and the length of this array is how many elements there are in it, that is, 5. It is worth mentioning that the subscript of the array, that is, the index is from start at 0 instead of 1

Step 5: Add Judgment Conditions

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
		var sex = [1, 2, 3, 4, 5,9,7,12,11]
			var sum = sex[0]
			for(var i=1;i<sex.length;i++){
			 if(sum<sex[i]){
				
			 }
			}
		</script>
	</body>
</html>

 The words here explain why I will define a variable above to put the first element of the array into it. The main purpose here is to compare each value of the array with each other by traversing the loop, and take the first value out and the other In comparison, if it is less than the value in the array, exchange the value, and continue to compare with the next one anyway.

Step 6: Add Judgment Conditions 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
		var sex = [1, 2, 3, 4, 5,9,7,12,11]
			var sum = sex[0]
			for(var i=1;i<sex.length;i++){
			 if(sum<sex[i]){
				 sum = sex[i]
			 }
			}console.log(sum);
		</script>
	</body>
</html>

 This will determine the maximum value in the array

The same is true for the minimum value, and the code is directly attached here.

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

			// 最小值
			 var sex = [21, 2, 3, 4, 5, 9, 7, 12, 11]
			 var sum = sex[0]
			 for (var i = 1; i < sex.length; i++) {
			 	if (sum > sex[i]) {
			 		sum = sex[i]
			 	}
			 }
			 console.log(sum);
			
			
			
		</script>
	</body>
</html>

 

Guess you like

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