Steps to find the sum of even numbers: 1. Store multiple numbers in an array, the syntax is "var array variable name = [value 1, value 2, value 3...];"; 2. Define a The variable is assigned a value of 0, which is used to store the summation result. The syntax is "var sum=0;"; 3. Use the for statement to traverse the array to find the even values, and use "=" to add the even values to sum, the syntax is " for(var i=0;i
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In javascript, you can store multiple numbers in an array, then traverse the array to find the even values and add them one by one to find the sum.
Implementation steps:
Step 1: Store multiple numbers into an array
var num =[52,58,79,46,13,52,68,45];
Step 2: Define a variable and assign it to 0 to store the summation result
var sum=0;
Step 3: Use the for statement to traverse Find the even values in the array and use "=" to add the even values to sum
for(var i = 0;i Copy after login
Extended knowledge: assignment operator
The assignment operator is used to assign values to variables. It has the following two forms:
Simple assignment operation =: put the right side of the equal sign The value of the operand is copied directly to the left operand, so the value of the left operand changes.
Assignment operation of additional operations: Before assignment, perform some operation on the right operand, and then copy the operation result to the left operand.
The assignment operations of some additional operations can realize the four arithmetic operations of addition, subtraction, multiplication and division. The specific description is as shown in the table:
Assignment operator | Explanation | Example | Equivalent to |
---|---|---|---|
= |
Addition or concatenation operation and assignment | a = b | a = a b |
-= |
Subtraction operation and assignment | a -= b | a= a - b |
*= |
Multiplication and assignment | a *= b | a = a * b |
/= |
Division operation and assignment | a /= b | a = a / b |
%= |
Modulo operation and assignment | a %= b | a = a % b |
The sample code is as follows:
var x = 10; x += 20; console.log(x); // 输出:30 var x = 12, y = 7; x -= y; console.log(x); // 输出:5 x = 5; x *= 25; console.log(x); // 输出:125 x = 50; x /= 10; console.log(x); // 输出:5 x = 100; x %= 15; console.log(x); // 输出:10
[Related recommendations:web front-end development]
The above is the detailed content of How to find the sum of even numbers in javascript. For more information, please follow other related articles on the PHP Chinese website!