Stuck on this exercise.
Create a program that picks a winner for a contest or prize drawing. Prompt for names of contestants until the user leaves the entry blank. Then randomly select a winner.
Example Output
Enter a name: Homer
Enter a name: Bart
Enter a name: Maggie
Enter a name: Lisa
Enter a name: Moe
Enter a name:
The winner is... Maggie.
Constraints
• Use a loop to capture user input into an array.
• Use a random number generator to pluck a value from the array.
• Don’t include a blank entry in the array.
• Some languages require that you define the length of the array ahead of time. You may need to find another data structure, like an ArrayList.
** New to programming
This is what I have done so far:
var name;
var users = [];
var winner = Math.floor(Math.random() * users.length + 1);
while (true) {
users.push(name = prompt("Enter a name:"));
if (name === "") {
break;
}
};
document.write(users + "<br>" + winner);
But the winner variable doesn't display anything but 1?