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

How to find the sum of even numbers in javascript

藏色散人
藏色散人Original
2021-07-03 09:11:144261browse

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>

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!

Statement:
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