-4

For my computer programming course in my highschool, I need to make a program for the end of term. Currently I am thinking of doing a Black Ops 2 load-out generator, and for that I need to generate random words in Javascript. I want to be able to generate a word, from a list of words. So if my list of words was this:

Horse,
Pig,
Dog,
Cat,
Parrot,
Iguana,

Then I would like to be able to generate one of those randomly, and then display it in a text box.

tshepang
  • 12,111
  • 21
  • 91
  • 136
John Eric
  • 29
  • 1
  • 1
  • 1
  • You've to try yourself to solve this problem. Then if you face any specific issues, you're welcome and we will help you. – Ikrom Apr 15 '13 at 14:32
  • 2
    The reason you got so many downvotes is because it doesn't appear that you put any effort whatsoever into actually trying to write your code. The members on this site can be extremely helpful, but they don't take kindly to being asked to do someone else's work. For future questions, first do research yourself to find the answer (looking at manuals/tutorials is a good start), then try writing the program yourself. If you then come across problems, ask here -- with your code, the error messages, and what you have tried to fix them -- and you should get a much better response. – Reinstate Monica -- notmaynard Apr 15 '13 at 14:41

2 Answers2

6
  1. Put these items in an array. var array = ["Horse", "Pig", "Dog", "Cat", "Parrot", "Iguana"];
  2. Generate a random number. var randInt = randomGenerator(0, array.length - 1); ( Generating random whole numbers in JavaScript in a specific range? )
  3. Use the random number to get an item from the array. var item = array[randInt];
  4. Use document.getElementById to get the textbox you want, and use .value = to set the textbox's value. var textbox = document.getElementById("textbox_id").value = randInt;
Community
  • 1
  • 1
Ian
  • 50,146
  • 13
  • 101
  • 111
  • +1 for step-by-step explanation. OP learns something today! – Reinstate Monica -- notmaynard Apr 15 '13 at 14:31
  • @iamnotmaynard Thanks :) I didn't want to write too much code as they didn't bother trying (or showed it) – Ian Apr 15 '13 at 14:33
  • 1
    sorry for not putting code, I don't understand a lot of javascript, im only a freshman. Thanks for the answer! – John Eric Apr 15 '13 at 14:36
  • 1
    By the way, for anyone implementing this great answer, I found that if step #2 returns the highest value in the randomization (in this case 6 for 6 items), step #3 will return `undefined` (since it's looking for ordinal index 6 in the array, which does not exist as there are not 7 items in the array). So, I removed the `+ 1` from the randomization code found in the link of #2: `return Math.floor(Math.random() * (max - min /*remove + 1*/)) + min;`, which properly returns indexes 0 through 5 (array items 1 through 6). Does anyone see a problem with this? – Aaron Jessen Mar 06 '15 at 20:46
  • 1
    @AaronJessen Not sure how I ever missed that. I'll update my answer to pass 1 less than the length of the array as the second param. Should accomplish the same thing – Ian Mar 06 '15 at 20:47
  • 1
    Yeah, I'm up to my eyeballs in a site launch, and for some reason, passing in one less didn't seem right. I really need coffee. – Aaron Jessen Mar 06 '15 at 22:03
2

You just use Math.random() and index into the array of words.

var words = ['Horse',
'Pig',
'Dog',
'Cat',
'Parrot',
'Iguana'
];
function randomWord(arr) {
    return arr[Math.floor(Math.random() * arr.length)];
}

for(var x=0; x<20; x++)
    console.log(randomWord(words));
Louis Ricci
  • 20,804
  • 5
  • 48
  • 62