简体   繁体   English

从数组中生成一个随机单词,然后将其打印到控制台登录 javascript 中的函数

[英]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我正在用javascript创建刽子手,我(我认为)成功地从函数中的数组中生成了一个随机词,以检查它是否有效我正在尝试将生成的词打印到控制台,但它似乎不是在这里工作是我的函数代码

    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这是关于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/演示: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() ).与其声明一个具有预定义长度的数组,不如声明一个空数组并在其末尾添加值(使用.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.您试图打印word (我将其重命名为getRandomWord ),它是/是一个函数。 You probably meant to use console.log(gameWordArray[randno]) , which should work.您可能打算使用console.log(gameWordArray[randno]) ,它应该可以工作。

That's what helped me to avoid duplicate alt tags for my online shop:这就是帮助我避免在线商店出现重复 alt 标签的原因:

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:我什至把它翻了一番,并且在独特的 alt 标签上有更多的范围:

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:最后,我的产品图库缩略图上的 alt-tags 看起来像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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