AKL012
AKL012

Reputation: 399

Adding names to an array and picking one out at random (Javascript)

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?

Upvotes: 1

Views: 763

Answers (4)

Taha Paksu
Taha Paksu

Reputation: 15616

You can't get the random index before the array is populated with names. First you use your while loop to collect names, then get the random index.

move this line:

var winner = Math.floor(Math.random() * users.length);

after the while loop.

You figured out the index, but you forgot to select the value from the array with the related index. Try adding:

document.write(users + "<br>" + users[winner]);

Upvotes: 2

Icepickle
Icepickle

Reputation: 12806

You are actually almost there.

The first thing that was wrong in your original post was the fact that you choose the user before the names were added. The choice have to be made after the input.

The second problem would have been that you added the + 1 to your random nr generator. That is not necessary, as javascript array's are 0 based, so for a length of 4 entries, the indexes are from 0 to 3.

And the final thing you were missing is to show the actual user instead of the number, for that you can access the users array and show the index you have chosen like users[winner]

If you want to make it a bit more sure, you could add that if no users were added, there can't be a winner ;) (by checking the users.length property)

var name;
var users = [];

while (true) {
  users.push(name = prompt("Enter a name:"));
  if (name === "") {
    break;
  }
};
// the winner can only be decided here (and + 1 is not necessary as Javascript has a 0 based index in arrays)
var winner = Math.floor(Math.random() * users.length);
// add the user array so you can get the index
// the join will make sure that your users are shown below eachother
alert(users.join('\n') + "\n" + users[winner] + ' won');

Upvotes: 2

AKL012
AKL012

Reputation: 399

Got it, thanks all.

var name;
var users = [];

while (true) {
  users.push(name = prompt("Enter a name:"));
  if (name === "") {
    break;
  }
};

var numUsers = users.length - 1;
var winner = Math.floor(Math.random() * numUsers + 1);

document.write("The winner is..." + users[winner]);

Upvotes: 0

kleopi
kleopi

Reputation: 460

Use a while-loop

I won't give you proper code, as this would ruin the exercise, but this is the idea:

setup array
  while (true)
  wait for input
  if input blank
      break out of loop
  else
      add entry to array
  end-while
  choose a winner
  end

Upvotes: 2

Related Questions