简体   繁体   English

来自数组的JavaScript随机选择器,具有“向数组添加名称”功能

[英]JavaScript Random Picker From Array with “Add Name to Array” Feature

Okay, so I have this JavaScript program. 好的,我有这个JavaScript程式。 I am trying to make a program that randomly picks two characters from Once Upon A Time and matches them. 我正在尝试制作一个程序,该程序从“从前”中随机选择两个字符并将其匹配。 I got that working fine, but am having trouble with the "Add Name to Array" feature. 我的工作正常,但是“将名称添加到阵列”功能遇到了麻烦。 Here is my code: 这是我的代码:

<center>
<b>
<input type="button" value="Randomize!" onclick="ouatRandomizer();">
<b>
<p id="text"></p>
<input id="name" type="text" placeholder="Add a Name" />
<input type="button" value="Add Array" onclick="addToArray();">
</center>

<script>

var nameInput   = document.getElementById("name");

var names = ["Hook", "Rumpelstiltskin", "Belle", "Emma", "Regina", "Aurora", "Elsa", "Anna", "Snow White", "Prince Charming", "Cora", "Zelena", "August", "Mulan", "Graham", "Discord", "Will", "Robin Hood", "Jiminy Cricket", "Henry", "Neal", "Red"];
var nameone = names[Math.floor(Math.random() * names.length)];
var nametwo = names[Math.floor(Math.random() * names.length)];
message = "Your characters are.. " + nameone + " and " + nametwo + ".";


function ouatRandomizer() {
    nameone = names[Math.floor(Math.random() * names.length)];
    nametwo = names[Math.floor(Math.random() * names.length)];
    message = "Your characters are.. " + nameone + " and " + nametwo + ".";
    document.getElementById("text").innerHTML = message;
}

function addToArray( name ) {
    names.push( name );
    console.log(names.join());
}



</script>

I would like it to be able to add any name in the input box and automatically randomize with that name. 我希望它能够在输入框中添加任何名称,并自动使用该名称进行随机化。 It does not have to remember the name, as that is complicated and would probably use a separate text file, but if you can do that, thank you. 它不必记住名称,因为名称很复杂,可能会使用单独的文本文件,但是如果可以的话,谢谢。

I thank you, again, for taking the time to read this and helping me solve my problem. 我再次感谢您抽出宝贵的时间阅读本文并帮助我解决了我的问题。 Two quick things I did want to mention now. 我现在想提两件事。 First, I am relatively new to these forums, so I do not know them very well. 首先,我对这些论坛还比较陌生,所以我不太了解它们。 Second, please try and explain the code if there is a lot in a brief, but descriptive way. 其次,请尝试以简短但描述性的方式解释代码。 I am still a beginner coder, and, as I said, new to the forums. 我仍然是初学者,并且,正如我所说,是论坛的新手。

Thank you. 谢谢。

I'll start off by mentioning that aside from Math.random() being a pseudo-random number generator, your randomization algorithm is biased towards the front of the array and will never select anything past "Graham". 我首先要提到的是,除了Math.random()是伪随机数生成器之外,您的随机化算法还偏向数组的前端,并且永远不会选择“ Graham”之后的任何东西。

You can fix this by using this randomiser instead: 您可以改用以下随机化器解决此问题:

/*
 * 
 * @param upper_bound is the total number of elements in your array.
 * 
 */
function randomize (upper_bound) {
    return Math.floor(Math.random() * upper_bound);
}

To answer your question, you have to need to grab the value of the input before you call the addToArray() function. 要回答您的问题,您需要在调用addToArray()函数之前获取输入的值。

Replace 更换

<input type="button" value="Add Array" onclick="addToArray();">

with

<button onclick="addInput()"></button>

Then add this function to your code: 然后将此函数添加到您的代码中:

function addInput () {
    var name = document.getElementById("name").value;
    addToArray(name);
}

Hope that helps. 希望能有所帮助。

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

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