【发布时间】:2019-07-02 11:17:20
【问题描述】:
这是测验的工作方式: 当用户键入 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();
我没有包含所有问题,因此代码不会很长。 问题 1-5 就是一个例子。
???代表阅读问题后获取用户输入
【问题讨论】:
-
您可以将问题属性连接为字符串:
console.log( question[i].number + ' - ' + question[i].question + ' - ' + ...) -
感谢您指出这一点
标签: javascript arrays node.js function