I am working on a code to calculate the average of four numbers entered by the user, but I want to exclude zeros from the calculation. Currently, the code successfully excludes one zero, but I am struggling to exclude multiple zeros. My initial approach was to remove any number containing a zero, so that the average is only calculated based on non-zero numbers. Here is the code snippet:
var num1 = parseInt(prompt("Enter the first number:"), 10);
var num2 = parseInt(prompt("Enter the second number:"), 10);
var num3 = parseInt(prompt("Enter the third number:"), 10);
var num4 = parseInt(prompt("Enter the fourth number:"), 10);
if (num1 == 0){
document.getElementById("output").textContent = ((num2 + num3 + num4) / 3);
}
if (num2 == 0){
document.getElementById("output").textContent = ((num1 + num3 + num4) / 3);
}
if (num3 == 0){
document.getElementById("output").textContent = ((num1 + num2 + num4) / 3);
}
if (num4 == 0){
document.getElementById("output").textContent = ((num1 + num2 + num3) / 3);
}
else {
document.getElementById("output").textContent = ((num1 + num2 + num3 + num4) / 4);
}