说明如何存储用户输入和点系统的javascript测试。

时间:2022-12-28 15:45:12

So I'm a bit new to coding and just needed direction, not necessarily doing it for me, but some steering.

所以我对编码有点陌生,只是需要方向,不一定是为我做,而是一些指导。

I need to make a personality quiz that takes user choices and based on their choices gives them a certain outcome out of 4 choices at the end.

我需要做一个性格测试,以用户的选择为基础,根据他们的选择,在最后的4个选择中给他们一个肯定的结果。

I know to create an array with questions and answers, but I'm not exactly clear what the next step is to set up a system to store user input.

我知道要创建一个有问题和答案的数组,但是我不清楚下一步是建立一个系统来存储用户输入。

Do I use another array to store the data?
Does that array have to refer to the buttons I make or the array earlier?

我是否使用另一个数组来存储数据?这个数组需要引用我之前创建的按钮或数组吗?

I'm not even sure if that's the right steps, I just need some clarification to the next step I should take after I create an array of my questions and answers.

我甚至不确定这是不是正确的步骤,我只是需要澄清一下我在创建一系列问题和答案后应该采取的下一步行动。

1 个解决方案

#1


0  

Using another array should be fine, I would create the array for answers with empty strings right at the beginning. You already know the length of both the questions and answers arrays should be the same. So something along the lines of:

使用另一个数组应该没问题,我将在开始时用空字符串创建数组。您已经知道问题和答案数组的长度应该是相同的。所以,沿着这条线

var answersArray = []; 
for (var i = 0; i < questionsArray.length; i++) {
  answersArray[i] = '';
}

After that you can use your buttons to fill the answersArray depending on the index that they belong. No matter the order in which the user answers the quiz, or if they don't fill all the answers. This way it will be easy for you to process the result in any way you need. All you need is an if statement that checks for the length:

之后,您可以使用您的按钮来根据它们所属的索引填充答案sarray。无论用户回答测试的顺序是什么,或者他们没有填写所有的答案。这样,您就可以轻松地处理您所需要的结果。您所需要的是一个if语句,用于检查长度:

if (answersArray[i].length > 0) {
  // do something ...
}

Note that this is a very simple approach that does not take into account saving the data in any way (cookies, localstorage or database).

注意,这是一种非常简单的方法,它不考虑以任何方式保存数据(cookie、localstorage或数据库)。

#1


0  

Using another array should be fine, I would create the array for answers with empty strings right at the beginning. You already know the length of both the questions and answers arrays should be the same. So something along the lines of:

使用另一个数组应该没问题,我将在开始时用空字符串创建数组。您已经知道问题和答案数组的长度应该是相同的。所以,沿着这条线

var answersArray = []; 
for (var i = 0; i < questionsArray.length; i++) {
  answersArray[i] = '';
}

After that you can use your buttons to fill the answersArray depending on the index that they belong. No matter the order in which the user answers the quiz, or if they don't fill all the answers. This way it will be easy for you to process the result in any way you need. All you need is an if statement that checks for the length:

之后,您可以使用您的按钮来根据它们所属的索引填充答案sarray。无论用户回答测试的顺序是什么,或者他们没有填写所有的答案。这样,您就可以轻松地处理您所需要的结果。您所需要的是一个if语句,用于检查长度:

if (answersArray[i].length > 0) {
  // do something ...
}

Note that this is a very simple approach that does not take into account saving the data in any way (cookies, localstorage or database).

注意,这是一种非常简单的方法,它不考虑以任何方式保存数据(cookie、localstorage或数据库)。