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>