简体   繁体   中英

Generate a random word from an array and then print it to the console log in a function in javascript

I am creating hangman in javascript and I have (I think) successfully generated a random word from an array in a function, to check if it works I am trying to print the generated word in to the console but it doesn't seem to be working here's my code for the function

    var word = function() //Random word is genereated from an array for the user to guess
{
  GameWordArray = new Array(7);
  GameWordArray[0] = "monitor";
  GameWordArray[1] = "program";
  GameWordArray[2] = "application";
  GameWordArray[3] = "keyboard";
  GameWordArray[4] = "javascript";
  GameWordArray[5] = "gaming";
  GameWordArray[6] = "network";
  randno = Math.floor(Math.random() * GameWordArray.length);
  document.write(GameWordArray[randno]);
  console.log(word);
}

Thanks in advance :)

Here is an example on jsfiddle

var words = ["monitor", "program", "application", "keyboard", "javascript", "gaming", "network"];

var word = words[Math.floor(Math.random() * words.length)];

console.log(word);

document.getElementById("word").textContent = word;

And to have it fit in directly with you present code:

var getRandomWord = function () {
    return words[Math.floor(Math.random() * words.length)];
};

Try using it this way:

var getRandomWord = (function () {
  var gameWordArray = [];
  gameWordArray.push("monitor");
  gameWordArray.push("program");
  gameWordArray.push("application");
  gameWordArray.push("keyboard");
  gameWordArray.push("javascript");
  gameWordArray.push("gaming");
  gameWordArray.push("network");
  return function () {
    var randNum, finalWord;
    randNum = Math.floor(Math.random() * gameWordArray.length);
    finalWord = gameWordArray[randNum];
    return finalWord;
  };
})();

DEMO: http://jsfiddle.net/bCEFA/1/

Instead of declaring an array with a predefined length, you might as well declare an empty one and add values to the end of it (with .push() ). You could've also declared the array like:

var gameWordArray = ["monitor", "program", ...];

You were trying to print word (which I renamed to getRandomWord ), which was/is a function. You probably meant to use console.log(gameWordArray[randno]) , which should work.

That's what helped me to avoid duplicate alt tags for my online shop:

var items = ['word1', 'word2', 'word3'];
var item = items[Math.floor(Math.random() * items.length)];

I even doubled it and had a much more range on unique alt tags:

var items2 = ['word4', 'word5', 'word6'];
var item2 = items2[Math.floor(Math.random() * items2.length)];

And in the end it looked like this for my alt-tags on my product gallery thumbnails:

markup += '<div class="product-image-thumbnail"><img alt="' + alt + ' ' + item + ' ' + item2 + '" title="' + title + '" src="' + image + '" /></div>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM