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

13,195

Solution 1

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)];
};

Solution 2

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.

Share:
13,195

Related videos on Youtube

Owen Brain
Author by

Owen Brain

Student, Kingston, studying computer science

Updated on September 15, 2022

Comments

  • Owen Brain
    Owen Brain over 1 year

    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 :)

    • Xotic750
      Xotic750 about 11 years
      Creating your random word in that manner is kind of painful as you create a new array of the words each time you call the function.
  • Owen Brain
    Owen Brain about 11 years
    Thanks but when I put this in place of my original code and try and run it, it gives me a syntax error elsewhere in the code It gives me this error on this line of code; Error detected: syntax error (#86) Error at or around line number 86 document.getElementById("button").onclick = guessButtonClicked;
  • Ian
    Ian about 11 years
    @OwenBrain Don't you think it would be helpful to provide the actual error or where the syntax error is referring to?
  • Owen Brain
    Owen Brain about 11 years
    Yeah I tried, sorry if it didn't come through properly I am new to this site, thanks