Advertisement
Spocoman

05. Sum Even Numbers

Jan 20th, 2022
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let sum = 0;
  3.     for (let i = 0; i < input.length; i ++){
  4.         Number(input[i]) % 2 === 0 ? sum += Number(input[i]) : sum;
  5.     }
  6.     console.log(sum);
  7. }
  8.  
  9. Решение със map(), filter() и reduce():
  10.  
  11. function solve(input) {
  12.  
  13.     let evenNum = input.map(Number).filter(a => a % 2 === 0);
  14.     let sum = evenNum.reduce((a, b) => a + b, 0);
  15.     console.log(sum);
  16.  
  17. }
  18.  
  19. Или:
  20.  
  21. function solve(input) {
  22.    
  23.     console.log(input.map(Number).filter(a => a % 2 === 0).reduce((a, b) => a + b, 0));
  24.  
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement