Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.

I'm having an issue not allowing zeros not to count. Because its adding to the array, and when the user types in 6+66+6, sum is 1212, average is calculating to 44 because of the extra 00 in the array.

I'm having an issue not allowing zeros not to count. Because its adding to the array, and when the user types in 6+6, sum is 12, average is calculating to 4 because of the extra 0 in the array.

I'm having an issue not allowing zeros not to count. Because its adding to the array, and when the user types in 6+6, sum is 12, average is calculating to 4 because of the extra 0 in the array.

Source Link

Validation for numeric data Javascript

I'm writing a program in Javascript that separates even and odd numbers, puts them into an array, adds the sum of numbers, and finds the average.

I'm having an issue not allowing zeros not to count. Because its adding to the array, and when the user types in 6+6, sum is 12, average is calculating to 4 because of the extra 0 in the array.

Is there anyway to not allow the zeros to count? Here is what I have so far..

var evenarray = []; var oddarray = []; var avgEven = 0; var avgOdd = 0; var isValid; function numberFunction(){ do { var numbers = prompt("Please enter numbers. Enter empty string to exit."); if(numbers % 2 == 0) { evenarray.push(numbers); var sumEven = 0; for (var i=0; i < evenarray.length; i++) { sumEven = sumEven + Number(evenarray[i]); } var avgEven = sumEven/evenarray.length; //alert("even"); } if(numbers % 2 !== 0) { oddarray.push(numbers); var sumOdd = 0; for (var i=0; i < oddarray.length; i++) { sumOdd = sumOdd + Number(oddarray[i]); } var avgOdd = sumOdd/oddarray.length; //alert("odd"); } //if(isNaN(numbers)){ //alert("Only numeric data only"); //} } while(numbers !== "");