Collect User Input Numbers

by isuru_nan

JavaScript

function collectNumbers() {
    let noOfInputs = 0;
    let userInput;
    let largest = -999999;
    let smallest = 999999;
    let total = 0;
    
    while (true) {
        userInput = parseInt(prompt("Enter a number (enter -999 to stop):"));
        
        if (userInput === -999) {
            break;
        }
        
        if (isNaN(userInput)) {
            alert("Please enter a valid number.");
            continue;
        }
        
		noOfInputs++;
        total += userInput;
        
        if (userInput > largest) {
            largest = userInput;
        }
        
        if (userInput < smallest) {
            smallest = userInput;
        }
    }
    
	console.log("Total of the numbers entered: " + total);
	console.log("No of times the user entered a number: " + noOfInputs);
	console.log("The largest number entered: " + largest);
	console.log("The smallest number entered: " + smallest);
}

collectNumbers();