Как сохранить ответ пользователя в массиве, чтобы он мог проверить с правильным ответом - PullRequest
0 голосов
/ 02 июля 2019

Вот как должен работать тест: когда пользователь вводит ответ, он должен сохранять ответ в массиве, чтобы можно было проверить правильные ответы в другом массиве.

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не включал все вопросы, поэтому код не будет очень длинным.Вопрос 1-5 является примером.

???означает получение пользовательского ввода после прочтения вопроса

1 Ответ

0 голосов
/ 02 июля 2019

Вот совет.Вместо того, чтобы создавать объекты для каждого вопроса явно, используйте эффективную структуру данных, чтобы сделать то же самое.Поскольку вы используете Node.js, вы можете попробовать файловое хранилище, используя модуль 'fs'.Храните ваши вопросы в виде массива объектов.Я бы предложил следующую схему для каждого объекта вопроса:

category: STRING,
question: STRING,
correctAnswers: [CORRECT ANSWER IDS],
options: [{
    id: INTEGER,
    statement: STRING,
    isCorrect: Boolean
}]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...