I am in the process of developing a program that prompts the user to enter values to store in an array. The program will continue to ask for more values until the user decides to stop entering numbers into the array. Once all the values are inputted, I need to display the array without any zeros (as users are permitted to include zeros) and calculate the sum of the array. One issue I have encountered with my current implementation is that arr.push(x)
seems to be attempting to add an undefined value in the function arrin. I believe there may be a better approach to solving this problem, so I am open to suggestions for improvement.
var x = parseInt(prompt("Enter a number, or type NaN to exit", "0"), 10);
var y = arrin(x);
var arr = [];
var s;
function arrin(x) {
if(x != NaN){
arr.push(x)
x = parseInt(prompt("Enter a number, or type NaN to exit", "0"), 10);
y = arrin(x);
}else{
document.write("<p>"+arr.toString()+"</p>");
s = sum(arr);
doucment.write("<p> The sum of all elements in the array is "+s+"</p>");
}
}