【问题标题】:How do i store the user's answer in an array so that it can check with the correct ans我如何将用户的答案存储在一个数组中,以便它可以检查正确的答案
【发布时间】: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


【解决方案1】:

这里有一个提示。与其明确地为每个问题创建对象,不如使用有效的数据结构来做同样的事情。由于您使用的是 Node.js,因此您可以使用“fs”模块尝试基于文件的存储。将您的问题存储为对象数组。我会为每个问题对象建议以下架构:

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

【讨论】:

  • sry 我是编程新手,所以我不太明白你的意思。可以更详细地解释一下吗?这在数组中是如何工作的?
  • 没问题。与整数数组类似,您可以循环对象数组。下面是一个例子:var array = [{ name: 'Han', age: 20 }, { name: 'Rohit', age: 22 } ]。当您遍历此数组时,您可以与单个对象进行交互。使用这个概念,您可以将您的问题与答案一起存储在一个数组中,每个对象都可以包含您的问题和一系列选项。是的,对象内的数组。我建议您使用一些公共 api 来掌握 JSON 及其工作原理。
  • 可以向我展示一个如何解决这个问题的例子吗?我做了一些修改。
  • 对node.js中的fs模块做一些研究。了解数组和对象如何协同工作。了解 JSON 以及如何处理 json 响应。那里有一些优秀的教程,了解核心概念并回到测验应用程序。
  • 另外,了解可用的 Array 方法,让您的生活更轻松。
猜你喜欢
  • 2020-07-03
  • 2019-01-24
  • 1970-01-01
  • 2018-06-28
  • 2018-10-05
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多