简体   繁体   English

我如何将用户的答案存储在数组中,以便可以检查正确的答案

[英]How do i store the user's answer in an array so that it can check with the correct ans

This is how the quiz should work: When the user type in the ans, it should store the ans in an array so that it can check with the correct ans in another array. 这是测验的工作方式:当用户键入ans时,应将ans存储在一个数组中,以便可以检查另一个数组中的正确ans。

var input = require("read_line-sync");
class Multiple_choice {
    constructor(number,question,choices,user_ans,correct_ans) {
        this.number = number; //question number
        this.question = question; //display question number
        this.choices = choices; //display question choices
        this.user_ans = user_ans; // store user ans
        this.correct_ans = correct_ans; // validate user ans
    }
}

class Quiz{
    quiz(category){// to store objects in different categories
        if (category==1){
            for(var i=0;i<5;i++){
                console.log(questions[i].number + questions[i].question + questions[i].choices + ???); // questions 1-5:History
            }

        }
        else if (category==2){
            for(var i=5;i<10;i++){
                console.log(questions[i].number + questions[i].question + questions[i].choices + ???); // questions 6-10: Chemistry
            }
        }
        else if (category==3){
            for(var i=10;i<15;i++){
                console.log(questions[i].number + questions[i].question + questions[i].choices +???); // question 11-15: Physics
            }
        }
    }
var questions =[new Multiple_Choice("Question 1: \n","There was an early settlement long before the arrival of Stamford Raffles. It was located between the mouth of the Singapore River and a small watercourse known today as Stamford Canal. What was the original name of this stream?","A. Fresh Water Well\n B. Fresh Water Lake\n C. Fish Water Spring\n D. Fresh Water Spring",user_ans,correct_ans),
                new Multiple_Choice("Question 2: \n","When did the first junk transporting immigrants from China arrive in Singapore?","1820\n 1821\n 1822\n 1823",user_ans,correct_ans),
                new Multiple_Choice("Question 3: \n","The earliest mosque in Singapore is believed to be","A. Jamar mosque\n B. Omar mosque \n C. Al Abram mosque \n D. Hajj ah Fatima mosque",user_ans,correct_ans)
                new Multiple_Choice("Question 4: \n","The oldest Jewish synagogue is the Mag both built in 1878. It is located at","\n A. Queen Street\n B. Waterloo Street \n C. Victoria Street\n D. cool Street",user_ans,correct_ans),
                new Multiple_Choice("Question 5: \n","The Sikhs make up a small but significant proportion of the Indian population of Singapore. They began arriving in Singapore from as early as 1881. Committed to their religion, they set about establishing temples. Today, the Central Sikh Temple is an imposing building, capped by an impressive dome. Where is it located?","\n A. Queen Street\n B. Silas Street\n C. Town Road \n D. mayflower Road",user_ans,correct_ans),
                new Multiple_Choice(),
                new Multiple_Choice(),
                new Multiple_Choice(),
                new Multiple_Choice(),
                new Multiple_Choice(),
                new Multiple_Choice(),
                new Multiple_Choice(),
                new Multiple_Choice(),
                new Multiple_Choice(),
                new Multiple_Choice()];
var name = new Quiz();
name.quiz();

i did not include all the questions so the code will not be very lengthy. 我没有包括所有问题,所以代码不会很冗长。 Question 1-5 is an example. 问题1-5是一个示例。

??? ??? stands for obtaining user input after reading the question 代表阅读问题后获得用户输入

Here's a tip. 这是一个提示。 Rather than creating objects for each question explicitly, use an efficient data structure to do the same. 与其明确地为每个问题创建对象,不如使用有效的数据结构来执行相同的操作。 Since you are using Node.js, you can try out file based storage using the 'fs' module. 由于您使用的是Node.js,因此可以使用“ fs”模块尝试基于文件的存储。 Store your questions as an array of objects. 将您的问题存储为一组对象。 I would suggest the following schema for each question object: 我会为每个问题对象建议以下架构:

category: STRING,
question: STRING,
correctAnswers: [CORRECT ANSWER IDS],
options: [{
    id: INTEGER,
    statement: STRING,
    isCorrect: Boolean
}]

暂无
暂无

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

相关问题 如何检查单击按钮的答案以查看它是否与 JavaScript 中的一组对象中找到的正确答案相匹配,以进行琐事游戏 - How do I check the answer of a clicked button to see if it matches the correct answer found within an array of objects in JavaScript for a trivia game 如何获取琐事游戏中每个按钮的索引并检查所选答案是否等于正确答案 - How do I get the index of each button in a trivia game and check if the selected answer is equal to the correct answer 我如何记录数组中正确元素的索引,答案不是 -1 - How can i log the index of the correct element in array with an answer that is NOT -1 我如何显示答案是否正确 - How do i show if an answer is correct or not 如何将标记为“价格”的所有 class 值存储到一个数组中以便对它们进行排序? - How do I store all my class values labeled "price" into an array so I can sort them? 如何在数组中存储单选按钮值并比较以得到正确答案? - How to store radio button value in array and compare to correct answer? 如何检查嵌套的 forLoop 中的所有元素是否为假并将答案返回/存储在变量中? 反应JS - How do I check if all elements are false within a a nested forLoop and return / store answer in a variable? React JS 如何将用户提示(输入)存储到数组中并检索其最大值和最小值? - How do I store a user prompt(input) into an array and retrieve it's max and min value? 如何为每个正确答案绘制简单图形 - How do i make a simple graph for each correct answer 在接受用户的答案之前,如何在控制台上显示我的问题? - How can I displaymy question to the console before I accept the user's answer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM