I wrote a code that takes in a list of numbers from the user and console logs the largest number, but it is not showing the correct output. Let me explain this code. This is a code that asks user for numbers (input) until they write quit. The numbers given by the user is stored in an array called numberList. Then I wrote a code that finds out the largest number in that array. But for some reason it doesn't work. Please help me out..
function add() {
var addNum = prompt(`What number would you like to add?`);
numberList.push(addNum);
}
function display() {
console.log(numberList);
}
var numberList = [];
var start = prompt(`Would you like to start the programme? y/n`);
var userInput;
if (start === 'y') {
while (userInput !== 'quit') {
userInput = prompt(`Action: add, display, quit`);
if (userInput === 'add') {
add();
} else if (userInput === 'display') {
display()
} else if (userInput === 'quit') {
userInput = 'quit';
alert(`Thank you for using the programme`)
} else {
alert(`Please enter a valid input`);
}
}
} else if (start === 'n') {
alert('As you say');
} else {
alert(`Please enter a valid input`);
}
var largestNum = 0;
for (i = 0; i < numberList.length; i++) {
if (numberList[i] > largestNum) {
largestNum = numberList[i];
}
}
console.log(`The largest number is ${largestNum}`);