Random winner selector I am looking to improve and simplify the way I code. What is the best way to represent this:



let names = [];
let name;

while(true) {
 name = prompt('Enter a name:');
  names.push(name);
  
  if(name === ''){
   names.pop();
   break;
  }
}

let winner = function(){
 return names[Math.floor(Math.random() * names.length)];
}

console.log(winner());
There is not much to be done about the random selector.  However, I would make the function accept an explicit parameter.

That input loop, though, is too complicated. It also fails to handle the "Cancel" button in any way.



function winner(names) {
    return names[Math.floor(Math.random() * names.length)];
}

let names = [];
for (let name; name = prompt('Enter a name:'); ) {
    names.push(name);
}
console.log(winner(names));