0

I have been trying to find a way to calculate the sum of even or odd numbers in an array. I figured out how to actually output only even or odd numbers in in the console, but I'm not sure how I would go about calculating the sum of even or odd numbers. The only solution I have found is to calculate the the whole array

My code most likely doesn't follow the best of code practices so be easy on me, but feel free to point out better ways to accomplish the result. I'm all ears.

The code under gives the current even numbers in array and outputs them to the browsers console

This is where I'm at so far

    <!DOCTYPE html>
    
    <html>
    <head>
      <meta charset="UTF-8">
      <title>For - loop </title>
      <script>
     window.onload = startFunction;

    function startFunction()
    {
        document.getElementById("buttonOne").onclick = buttonFunction;
    }

    function buttonFunction ()
    {
        var list=[22,45,63,223,12,56,89];
        var number = 0;
        var rest = 0;
        var counter;
        
        for(counter=0;counter<20;counter+=1)
        {
        
            number = list[counter];
            
            rest = number%2;
            
            if(rest == 0)  //type (rest==0) to find even numbers in the array
            {
                console.log(number);
            }
        }
    }
  </script>
</head>
<body>
    <button id="buttonOne">Enter</button>
</body>
</html>
Unmitigated
  • 76,500
  • 11
  • 62
  • 80

3 Answers3

1

You can do this quite simply with Array#reduce.

const list=[22,45,63,223,12,56,89];
let evenSum = list.reduce((acc,curr)=>acc + (curr % 2 == 0 ? curr : 0), 0);
console.log('Even sum:', evenSum);
let oddSum = list.reduce((acc,curr)=>acc + (curr % 2 == 1 ? curr : 0), 0);
console.log('Odd sum:', oddSum);

If performance is particularly important, you can calculate both sums with a single reduce operation.

const list=[22,45,63,223,12,56,89];
let [evenSum, oddSum] = list.reduce((acc,curr)=>(acc[curr % 2] += curr, acc), [0, 0]);
console.log('Even sum:', evenSum);
console.log('Odd sum:', oddSum);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

let list = [22,45,63,223,12,56,89];
let number = 0, rest = 0, result = 0;

for(let counter = 0; counter < 20; counter += 1) {
  number = list[counter];
  rest = number%2;
  if(rest == 0) {
    result += number; //here
  }
}

console.log(result); //printing result
sercan
  • 182
  • 19
  • Is there a benefit to user "let" istead of "var"? – im_bad_at_this Dec 02 '20 at 19:21
  • @im_bad_at_this [Is there a performance difference between 'let' and 'var' in JavaScript](https://stackoverflow.com/questions/21467642/is-there-a-performance-difference-between-let-and-var-in-javascript) – sercan Dec 02 '20 at 19:24
0

Technically you can just loop through the array and if even, add that to a variable else add that to a different variable.

list = [22, 45, 63, 223, 12, 56, 89]
evensum = 0;
oddsum = 0;

list.forEach(function(e) {
  if (e % 2 == 0) {
    evensum += e;
  } else{
    oddsum += e;
  }
});

console.log(evensum, oddsum)
imvain2
  • 15,480
  • 1
  • 16
  • 21