Home > Web Front-end > JS Tutorial > How to find the sum of even numbers in javascript

How to find the sum of even numbers in javascript

藏色散人
Release: 2021-07-03 09:11:14
Original
4306 people have browsed it

How to find the sum of even numbers in JavaScript: First open the corresponding code file; then pass "for(i = 0;i

How to find the sum of even numbers in javascript

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

How to find the sum of even numbers in javascript?

Finding the sum of even numbers in js

The code is as follows:

<script>
  var sum =[52,58,79,46,13,52,68,45]
  document.write(sum.sort()+"<hr>");
  
   var num = 0;
  for(i = 0;i<sum.length;i++){
   if(sum[i]%2 ==0){
     num+=sum[i]
   }
  }
 document.write("偶数和为"+num)
 </script>
Copy after login

Recommended study: "javascript Advanced Tutorial"

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