Output Even Numbers In an Array

I want to write a program that receives a list of numbers, converts them into an array, and output the even numbers in the array with Modulus Operator and For Loop.

The code does not run as expected:

function even_num(){
  var submission = prompt("Enter Numbers:", ""); // receive list of numbers 
  var subarray = submission.split('');  // convert to array

  for(var i =1; i < subarray.length; i++){

    if( subarray[i] % 2 != 0){
      document.getElementById("return").innerHTML += subarray[i] + ",";
    }
  }
}
<button onclick="even_num()">Even Number in an Array</button>
<p id = "return"></p>

Thank you



Read more here: https://stackoverflow.com/questions/64948912/output-even-numbers-in-an-array

Content Attribution

This content was originally published by Sina Heidari at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: