简体   繁体   English

根据特定的数组字符串检查输入

[英]Checking an input against a specific array string

I am trying to create a quiz that randomly selects a question from pool of questions in an array, which is answered in an input box that is to be checked against the corresponding answer string in the array. 我正在尝试创建一个从阵列中的问题池中随机选择一个问题的测验,将在输入框中进行回答,该输入框将根据阵列中的相应答案字符串进行检查。 I used math.floor(math.random() *); 我用math.floor(math.random()*); to get my random number. 得到我的随机数。 The random number is intended to be used to find both the question and answer, which are arranged in order to correspond to one another, eg ['question1','question2'] and ['answer1','answer2']. 随机数旨在用于查找问题和答案,它们被安排为彼此对应,例如['question1','question2']和['answer1','answer2']。

I am having difficulty with trying to get my input to properly be checked against the corresponding answer value from the array. 我很难尝试根据数组中的相应答案值正确检查我的输入。 I am fairly novice at Javascript, so I am not sure as to how to do this. 我是Java语言的新手,因此不确定如何执行此操作。 I tried using the document.getElementById command to compare the two. 我尝试使用document.getElementById命令比较两者。 I suspect it has something to do with the fact that ansNum doesn't get the value of questNum because of the fact that questNum is only given its value inside the generateQuiz function. 我怀疑这与ansNum无法获得questNum的值有关,因为questNum仅在generateQuiz函数中获得了它的值。 (I realize ansNum is likely redundant, but I was just playing around to see if anything would happen) (我意识到ansNum可能是多余的,但我只是在玩耍看看是否会发生任何事情)

Javascript: Javascript:

const questions = ['What do young Roman males wear?','Who is the Roman god of the smith?','Who is the 6th king of Rome?'];

const answers = ['toga praetexta','vulcan','servius tullius'];

function getQuestNum() {
 questNum = Math.floor(Math.random() * 3);
};

function getAnsNum() {
ansNum = questNum();
}

function generateQuiz() {
getQuestNum();
document.getElementById("question").innerHTML = questions[questNum];
};

function checkCorrect() {
getAnsNum();
if (answer[ansNum] = document.getElementById("input").innerHTML) {
        document.getElementById("verification").innerHTML = "Correct!";
        }
};

Codepen Link Codepen链接

An image of the code 代码图片

It would be simpler to create an array of objects that each contain both a question and an answer - and create a function that generates your random number and returns the object at the corresponding index. 创建一个同时包含一个问题和一个答案的对象数组,并创建一个生成随机数并在相应索引处返回对象的函数,会更简单。

Then you'll have access to everything you need without worrying about whether or not you can maintain access to the original randomly selected number, or matching up indices between two different arrays. 然后,您将可以访问所需的所有内容,而不必担心是否可以访问原始随机选择的数字,还是可以在两个不同的数组之间匹配索引。

Based on your code, I fixed it with some changes. 根据您的代码,我通过一些更改对其进行了修复。 It is not the best way to do this i think. 我认为这不是最好的方法。 I posted the js part here. 我在这里发布了js部分。

const questions = ['What do young Roman males wear?','Who is the Roman god of the smith?','Who is the 6th king of Rome?'];

const answers = ['toga praetexta','vulcan','servius tullius'];
var questNum;
function getQuestNum() {
    questNum = Math.floor(Math.random() * 3);
};

function getAnsNum() {
    ansNum = questNum;
}

function generateQuiz() {
    getQuestNum();
    document.getElementById("question").innerHTML = questions[questNum];
};

function checkCorrect() {
    getAnsNum();
    if (answers[ansNum] = document.getElementById("input").value) {
        document.getElementById("verification").innerHTML = "Correct!";
    }
};
  • First you need a global variable questNum then you can use it in all of your functions. 首先,您需要一个全局变量questNum,然后可以在所有函数中使用它。

  • The function getAnsNum() is redundant, at least i think so, just use questNum in your checkCorrect() function. 函数getAnsNum()是多余的,至少我认为是这样,只需在您的checkCorrect()函数中使用questNum即可。

  • For getElementByID function, insert an ID attribute to your input 对于getElementByID函数,在输入中插入一个ID属性

     <input id="input" type="text" name="input"> 
  • For input, if you want to take the value of the input field, use document.getElementById("input").value instead of innerHTML. 对于输入,如果要获取输入字段的值,请使用document.getElementById(“ input”)。value代替innerHTML。

  • If you not sure about any result, console.log it or use Chrome dev debug tool to check the result. 如果不确定任何结果,请console.log它或使用Chrome dev调试工具检查结果。 In the checkCorrect function, your array name should be answers instead of answer. 在checkCorrect函数中,您的数组名称应该是答案而不是答案。

Shorter ver: 短版:

const questions = ['What do young Roman males wear?','Who is the Roman god of the smith?','Who is the 6th king of Rome?'];

const answers = ['toga praetexta','vulcan','servius tullius'];
var questNum;
function getQuestNum() {
    questNum = Math.floor(Math.random() * 3);
};

function generateQuiz() {
    getQuestNum();
    document.getElementById("question").innerHTML = questions[questNum];
};

function checkCorrect() {
    if (answers[questNum] = document.getElementById("input").value) {
            document.getElementById("verification").innerHTML = "Correct!";
            }
};

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

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