简体   繁体   English

如何在另一个区域再次显示随机生成的字符串(来自数组)?

[英]How to display a randomly generated string (from array) again in another area?

Basically, I'm using HTML and JavaScript to make a simple rock, paper, scissors game where a button activates a randomly generated string from an array (rock, paper, or scissors).基本上,我使用 HTML 和 JavaScript 来制作一个简单的石头剪刀布游戏,其中一个按钮激活从数组(石头、剪刀布或布)中随机生成的字符串。 I already have it so that the button randomly generates a computer choice and it displays my choice (using radio buttons) in a box below.我已经有了它,因此该按钮会随机生成一个计算机选择,并在下面的框中显示我的选择(使用单选按钮)。 I'm stuck because I don't know how to display the randomly generated computer choice again in another box.我被卡住了,因为我不知道如何在另一个框中再次显示随机生成的计算机选择。

I tried using if else statements but I can't figure out how to reference the randomly chosen string from the array.我尝试使用 if else 语句,但我不知道如何从数组中引用随机选择的字符串。 Here is what I tried before within the html game:这是我之前在 html 游戏中尝试过的:

<script>
    function myFunction();{
    if (document.getElementById("randomSelect")) = 'rock'{
        document.getElementById('computerChoice').innerHTML = 'rock'
}       if (document.getElementById("randomSelect")) = 'paper'{
        document.getElementById('computerChoice').innerHTML = 'paper'
}       if (document.getElementById("randomSelect")) = 'scissors'{
        document.getElementById('computerChoice').innerHTML = 'scissors'
}
}
</script>
<p id = "computerChoice"></p>

Also, sorry if this makes no sense.另外,抱歉,如果这没有意义。 I'm new to coding.我是编码新手。

Generate random number between 0 and 2 (Array Index).生成 0 到 2 之间的随机数(数组索引)。 Store items in an array and pull out based on random Index generated.将项目存储在数组中并根据生成的随机索引拉出。

 var items = ["rock", "paper", "scissors"]; document.getElementById("randomSelect").addEventListener('click', myFunction); function myFunction() { /*Generate random number between 0 and 2*/ var randomNum = Math.floor(Math.random() * 3); /*Set the random Item*/ document.getElementById('computerChoice').innerHTML = items[randomNum]; }

 <p id="computerChoice"></p> <button type="button" id="randomSelect">Random</button>

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

相关问题 如何使用 Javascript 从随机生成的数字数组中省略 0? - How to omit 0 from array of randomly generated numbers using Javascript? 单击后如何使用 JavaScript 再次重置随机生成的数字? - How to reset a randomly generated number again after a click using JavaScript? 如何从for循环中随机选择颜色和形状并将其显示在另一个div中? - How to randomly pick colors and shapes from a for loop and display it in another div? 计算数组中随机生成的元素? - count randomly generated element from an array? 来自数组的随机生成字母的问题 - Problem with randomly-generated letters from Array 如何从 object 的数组中仅随机显示一张 1 图像 - how to randomly display only one 1 image from the array of object 如何从数组中随机获取字符串项目并将字符串单词的每个字符随机放入li标签 - How to get random string items from an array randomly and put each character of the string word into li tags randomly 单击按钮时如何显示来自特定API的新随机生成的报价? - How to display a newly randomly generated quote from a specific API when clicking a button? 根据URL从数组中随机显示 - Display randomly from array based on URL 如何显示分数计数,显示其随机生成的时间? - How to display a score count, displaying times it has been randomly generated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM