[英]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.