Arrays 当存在多个数组时,如何处理math.random?
我有四个阵列-花,树,动物和歌曲 我有四个相应的函数-flower()、tree()、critter()和song() 在上面的每个函数中,我调用了math.random函数从特定数组中检索一个随机字。例如,在my flower()函数中,我有:Arrays 当存在多个数组时,如何处理math.random?,arrays,math,random,floor,Arrays,Math,Random,Floor,我有四个阵列-花,树,动物和歌曲 我有四个相应的函数-flower()、tree()、critter()和song() 在上面的每个函数中,我调用了math.random函数从特定数组中检索一个随机字。例如,在my flower()函数中,我有: answer = flowers[Math.floor(Math.random() * flowers.length)]; 我发现为了生成随机词,我还需要全局运行math.random函数。这是我的问题。我不知道如何在不指定变量的情况下创建math.
answer = flowers[Math.floor(Math.random() * flowers.length)];
我发现为了生成随机词,我还需要全局运行math.random函数。这是我的问题。我不知道如何在不指定变量的情况下创建math.random函数。因此,我有以下几点:
function randomWord() {
answer = songs[Math.floor(Math.random() * songs.length)];
}
因此,有时当我在花卉类别中时,我的花卉阵列中的一个词会出现,但有时会出现一首歌曲,因为我必须将其发布到全球,以使其发挥作用
我不知道如何修复它,虽然关于数组的math.floor函数有很多问题,但我还没有找到任何关于如何处理这样的多个数组的信息
我想知道对于全局调用的math.floor函数,是否有一个通用的词可以代替一个特定的变量词?或者一个if-else语句?我被困住了,希望得到任何指导。多谢各位
编辑以添加:
@KevinWallis这是直接从我的代码粘贴的:
let answer = "";
function randomWord() {
answer = songs[Math.floor(Math.random() * songs.length)];
}
function flower() {
document.getElementById("main").style.display = 'none';
document.getElementById("play").style.display = "block";
answer = flowers[Math.floor(Math.random() * flowers.length)];
word = flowers[answer];
document.getElementById("categoryName").innerHTML = "Types of Flowers";
updateTreePicture();
randomWord();
generateButtons();
guessedWord();
}
更新:我只是想发布修复的结果:
function randomWord() {
if (categoryName === 'Flowers') {
answer = flowers[Math.floor(Math.random() * flowers.length)];
} else if (categoryName === 'Trees') {
answer = trees[Math.floor(Math.random() * trees.length)];
} else if (categoryName === 'Critters') {
answer = critters[Math.floor(Math.random() * critters.length)];
} else if (categoryName === 'Songs') {
answer = songs[Math.floor(Math.random() * songs.length)];
}
}
您可以为歌曲、鲜花等创建多个
random
函数。下面是调用此类随机函数并使用返回值的示例
function randomSong() {
songs = ...;
return songs[Math.floor(Math.random() * songs.length)];
}
function randomFlower() {
flowers = ...;
return flowers[Math.floor(Math.random() * flowers.length)];
}
...
function flower() {
document.getElementById("main").style.display = 'none';
document.getElementById("play").style.display = "block";
// use the selected flower instead of the global answer for all
flower = randomFlower();
document.getElementById("categoryName").innerHTML = "Types of Flowers";
// e.g. the flower can be used as parameter
updateTreePicture(flower);
generateButtons();
guessedWord();
}
您可以为歌曲、鲜花等创建多个
random
函数。下面是调用此类随机函数并使用返回值的示例
function randomSong() {
songs = ...;
return songs[Math.floor(Math.random() * songs.length)];
}
function randomFlower() {
flowers = ...;
return flowers[Math.floor(Math.random() * flowers.length)];
}
...
function flower() {
document.getElementById("main").style.display = 'none';
document.getElementById("play").style.display = "block";
// use the selected flower instead of the global answer for all
flower = randomFlower();
document.getElementById("categoryName").innerHTML = "Types of Flowers";
// e.g. the flower can be used as parameter
updateTreePicture(flower);
generateButtons();
guessedWord();
}
我认为这里有一些主要的东西有助于您理解,并且可能会阻止您将注意力集中在将所有内容存储在一个全局变量中,作为访问任何地方的数据的方法:
console.log(单号)代码>我认为这里有一些主要的东西有助于您理解,并且可能会阻止您将注意力集中在将所有内容存储在单个全局变量中,作为访问任何地方的数据的方法:
console.log(单号)你能显示你调用
randomWord()的代码吗
函数?我不确定我是否理解您的问题,但可能return
的概念会对您有所帮助。根据您选择的随机列表的条件,以及在哪里调用flower
函数?您能否显示调用randomWord()的代码
函数?我不确定我是否理解您的问题,但可能返回
的概念将帮助您。根据您选择的随机列表的条件,以及在哪里调用花
函数?谢谢。虽然这确实有效,但在代码中其他需要randomWord()的地方会产生一些问题。例如-重试(页面刷新)按钮。然后,我必须为每个类别分配一个唯一的刷新按钮。在本例中我可以这样做,但对于具有更多阵列选项的项目来说,这似乎很麻烦。我很欣赏这个答案,并将继续寻找一个解决方案,当所有类别都需要相同的操作时,不需要为每个类别分配单独的代码-我相信它是存在的!再次感谢!非常感谢。虽然这确实有效,但在代码中其他需要randomWord()的地方会产生一些问题。例如-重试(页面刷新)按钮。然后,我必须为每个类别分配一个唯一的刷新按钮。在本例中我可以这样做,但对于具有更多阵列选项的项目来说,这似乎很麻烦。我很欣赏这个答案,并将继续寻找一个解决方案,当所有类别都需要相同的操作时,不需要为每个类别分配单独的代码-我相信它是存在的!再次感谢!非常感谢-我感谢所有这些伟大的信息!没问题!很乐意提供帮助。@jenkruk那么这两个答案中的哪一个帮助解决了你的问题?@RokoC.Buljan-请参阅我文章的最后一节,在那里我用解决方案更新了它(if…else语句)。谢谢。非常感谢-我非常感谢所有这些伟大的信息!没问题!很乐意提供帮助。@jenkruk那么这两个答案中的哪一个帮助解决了你的问题?@RokoC.Buljan-请参阅我文章的最后一节,在那里我用解决方案更新了它(if…else语句)。非常感谢。