Reputation: 33
I'm trying to make a javascript that picks a random string out of an array, and it does that, but after the output it always says "unidentified". It wouldn't be a big problem, except for the fact that i'm trying to do that 10-20 times, which makes the 10-20 "unidentifieds" get in the way while trying to read the strings.
//Draw random contestants for a game show!
var contestants = ["Alejandro", "Amy", "AnnMaria", "B", "Beardo", "Beth", "Blainley", "Brick", "Bridgette", "Cameron", "Cody", "Courtney", "Dakota", "Dave", "Dawn", "DJ", "Duncan", "Ella", "Eva", "Ezekiel", "Geoff", "Gwen", "Harold", "Heather", "Izzy", "Jasmine", "Jo", "Justin", "Katie", "Leonard", "Leshawna", "Lightning", "Lindsey", "Max", "Mike", "Noah", "Owen", "Rodney", "Sadie", "Sam", "Samey", "Scarlett", "Scott", "Shawn", "Sierra", "Sky", "Staci", "Sugar", "Tofur", "Trent", "Tyler", "Zoey"];
var randomContestant = function () {
console.log(contestants[Math.floor(Math.random() * 52)]);
};
//randomContestant(); to pick a random contestant.
var drawContestants = function (howMany) {
for (var i = 0 ; i < howMany; i++) {
console.log (randomContestant(i + howMany));
}
};
//drawContestants(numberOfContestantsYouWantDrawn); to draw a number of random contestants.
From the other questions i looked at, it has to do with the fact that the argument for the function is blank? If that is the problem, what should i put there so make it stop giving "Unidentified" after the output?
(I have only been doing javascript for a couple of days now, so if this question has been answered, I either missed that one, or couldn't understand their answer to it since it was more advanced.)
Upvotes: 3
Views: 64
Reputation: 371049
You're passing a parameter to randomContestant
but you're not using it in the function there. You're also not removing the contestants as they're selected. Try something like this instead:
const contestants = ["Alejandro", "Amy", "AnnMaria", "B", "Beardo", "Beth", "Blainley", "Brick", "Bridgette", "Cameron", "Cody", "Courtney", "Dakota", "Dave", "Dawn", "DJ", "Duncan", "Ella", "Eva", "Ezekiel", "Geoff", "Gwen", "Harold", "Heather", "Izzy", "Jasmine", "Jo", "Justin", "Katie", "Leonard", "Leshawna", "Lightning", "Lindsey", "Max", "Mike", "Noah", "Owen", "Rodney", "Sadie", "Sam", "Samey", "Scarlett", "Scott", "Shawn", "Sierra", "Sky", "Staci", "Sugar", "Tofur", "Trent", "Tyler", "Zoey"];
const randomContestant = function (pool) {
return pool[Math.floor(Math.random() * pool.length)];
};
const drawContestants = function (howMany) {
// make a copy so as not to mutate the original array
let contestantPool = contestants.slice();
for (let i = 0 ; i < howMany; i++) {
const pick = randomContestant(contestantPool);
console.log(pick);
const index = contestantPool.indexOf(pick);
contestantPool =
[...contestantPool.slice(0, index),
...contestantPool.slice(index + 1),
];
}
};
drawContestants(10);
Upvotes: 1
Reputation: 5472
You need to return
the value. You are only printing out the value.
const a = [];
for(let i=0;i<52;i++)
a[i] = i;
var randomArrayItem = function (arr) {
return arr[Math.floor(Math.random() * 52)];
};
console.log(randomArrayItem(a));
Upvotes: 2
Reputation: 2351
JavaScript functions will by default return undefined.
You could try restructuring your code so that you console log 52 times form a loop, instead of calling a function that console logs 52 times.
Upvotes: 1