Skip to content

Instantly share code, notes, and snippets.

@stahnni
Last active May 14, 2017 05:38
Show Gist options
  • Save stahnni/b9925fb0bbae12aec10feb696f891842 to your computer and use it in GitHub Desktop.
Save stahnni/b9925fb0bbae12aec10feb696f891842 to your computer and use it in GitHub Desktop.
Sum input numbers
/*
Write the function sumInput() that:
Asks the user for values using prompt and stores the values in the array.
Finishes asking when the user enters a non-numeric value, an empty string, or presses “Cancel”.
Calculates and returns the sum of array items.
P.S. A zero 0 is a valid number, please don’t stop the input on zero.
*/
let sumArr = [];
function sumInput() {
while (true) {
//prompt user for a number
let input = Number(prompt("Please enter a number"));
//check to see if entry is a number or not
if(input === "" || input === null || !isFinite(input)) break;
//push the input into the array
sumArr.push(input);
}
let sum = 0;
for(let number of sumArr){
sum+=number;
console.log(sum);
}
}
console.log(sumInput());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment