Home > Web Front-end > Front-end Q&A > How to find the sum of even numbers in javascript

How to find the sum of even numbers in javascript

青灯夜游
Release: 2022-10-10 17:09:59
Original
2665 people have browsed it

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

How to find the sum of even numbers in javascript

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];
Copy after login

How to find the sum of even numbers in javascript

Step 2: Define a variable and assign it to 0 to store the summation result

var sum=0;
Copy after login

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<num.length;i++){
	if(num[i]%2 ==0){
	 sum+=num[i];
	}
}
console.log("偶数值的和为:"+sum);
Copy after login

How to find the sum of even numbers in javascript

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 that implements the additional operations of the four arithmetic operations of addition, subtraction, multiplication and division
Assignment operator ExplanationExampleEquivalent to
=Addition or concatenation operation and assignmenta = ba = a b
-=Subtraction operation and assignmenta -= ba= a - b
*=Multiplication and assignmenta *= ba = a * b
/=Division operation and assignmenta /= ba = a / b
%=Modulo operation and assignmenta %= ba = 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
Copy after login

How to find the sum of even numbers in javascript

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template