【问题标题】:Getting and randomizing questions from JSON file doesn't produce expected output从 JSON 文件中获取和随机化问题不会产生预期的输出
【发布时间】:2018-02-07 10:02:04
【问题描述】:

我按照网上的教程for my Quiz.

我想随机化问题,只从我的 questions.json 文件中得到 10 个。我做错了什么?

    ionViewDidLoad() {
    this.slides.lockSwipes(true);
    this.dataService.load().then((data) => {
      data.map((question) => {
       //Added this for the questions to be randomized
        let questionOrder = question.questionText;
        question.questionText = this.randomizeQuestions(questionOrder);

        let originalOrder = question.answers;
        question.answers = this.randomizeAnswers(originalOrder);
        return question;
      });
      this.questions = data;
    });
  }

  nextSlide() {
    this.slides.lockSwipes(false);
    this.slides.slideNext();
    this.slides.lockSwipes(true);
  }

  selectAnswer(answer, question) {
    this.hasAnswered = true;
    answer.selected = true;
    question.flashCardFlipped = true;

    if (answer.correct) {
      this.score++;
      this.passingscore = + this.score;
    }

    setTimeout(() => {
      this.hasAnswered = false;
      this.nextSlide();
      answer.selected = false;
      question.flashCardFlipped = false;
    }, 1000);
  }

 //Added this for my questions to be randomize which I tried to imitate the randomization of the answers
  randomizeQuestions(rawQuestions: any[]): any[] {
    for(let i = rawQuestions.length-1; i<10;  i++){
      let j = Math.floor(Math.random() * i);
      let temp = rawQuestions[i];
      rawQuestions[i] = rawQuestions[j];
      rawQuestions[j] = temp;
    }
    return rawQuestions;
  }

  randomizeAnswers(rawAnswers: any[]): any[] {
    for (let i = rawAnswers.length - 1; i > 0; i--) {
      let j = Math.floor(Math.random() * (i + 1));
      let temp = rawAnswers[i];
      rawAnswers[i] = rawAnswers[j];
      rawAnswers[j] = temp;
    }
    return rawAnswers;
  }

我在教程中使用的 JSON 示例:

{
"questions": [
    {
        "flashCardFront": "<img src='assets/images/animals.png'/>",
        "flashCardBack": "",
        "flashCardFlipped": false,
        "questionText": "These breathe, feed, grow, and leave offspring.",
        "answers": [
            {"answer": "Non-living Things", "correct": false, "selected": false},
            {"answer": "Living Things", "correct": true, "selected": false},
            {"answer": "None of the above", "correct": false, "selected": false}
        ]
    },
     {
        "flashCardFront": "<img src='assets/images/universe.png' />",
        "flashCardBack": "",
        "flashCardFlipped": false,
        "questionText": "It is the only world that supports life.",
        "answers": [
            {"answer": "Mars", "correct": false, "selected": false},
            {"answer": "Universe", "correct": false, "selected": false},
            {"answer": "Earth", "correct": true, "selected": false}
        ]
    },
    {
        "flashCardFront": "<img src='assets/images/earth.png' />",
        "flashCardBack": "",
        "flashCardFlipped": false,
        "questionText": "It was a water world with small areas of dry land",
        "answers": [
            {"answer": "Old Earth", "correct": false, "selected": false},
            {"answer": "Young Earth", "correct": false, "selected": false},
            {"answer": "Earth", "correct": true, "selected": false}
        ]
    },
    {
        "flashCardFront": "<img src='assets/images/earth.png' />",
        "flashCardBack": "",
        "flashCardFlipped": false,
        "questionText": "According to studies of ancient rocks, life began on Earth about how many years ago?",
        "answers": [
            {"answer": "5 million years", "correct": false, "selected": false},
            {"answer": "3300 million years", "correct": false, "selected": false},
            {"answer": "3800 million years", "correct": true, "selected": false}
        ]
    },
    {
        "flashCardFront": "<img src='assets/images/animals.png' />",
        "flashCardBack": "",
        "flashCardFlipped": false,
        "questionText": "They range from the simplest single-celled bacteria to plants, animals, and humans.",
        "answers": [
            {"answer": "Living things", "correct": true, "selected": false},
            {"answer": "Non-living things", "correct": false, "selected": false},
            {"answer": "Cells", "correct": false, "selected": false}
        ]
    },
    {
        "flashCardFront": "<img src='assets/images/earth.png' />",
        "flashCardBack": "",
        "flashCardFlipped": false,
        "questionText": "Comets and meteors rained down on what planet?",
        "answers": [
            {"answer": "Earth", "correct": true, "selected": false},
            {"answer": "Jupiter", "correct": false, "selected": false},
            {"answer": "Venus", "correct": false, "selected": false}
        ]
    },
    {
        "flashCardFront": "<img src='assets/images/earth.png' />",
        "flashCardBack": "",
        "flashCardFlipped": false,
        "questionText": "Many scientists believe that life began here.",
        "answers": [
            {"answer": "Lakes and oceans", "correct": true, "selected": false},
            {"answer": "Planet Earth", "correct": false, "selected": false},
            {"answer": "Rivers and mountains", "correct": false, "selected": false}
        ]
    },
    {
        "flashCardFront": "<img src='assets/images/earth.png' />",
        "flashCardBack": "",
        "flashCardFlipped": false,
        "questionText": "These are energy that came from hot springs on the seabed.",
        "answers": [
            {"answer": "Black Matter", "correct": false, "selected": false},
            {"answer": "Black Smokers", "correct": true, "selected": false},
            {"answer": "Molecular Deposit", "correct": false, "selected": false}
        ]
    },
    {
        "flashCardFront": "<img src='assets/images/earth.png' />",
        "flashCardBack": "",
        "flashCardFlipped": false,
        "questionText": "It is the theory of the beginning of the universe.",
        "answers": [
            {"answer": "The Big Boom Theory", "correct": false, "selected": false},
            {"answer": "The Big Bang Theory", "correct": true, "selected": false},
            {"answer": "The Beginning Theory", "correct": false, "selected": false}
        ]
    }
    ,
    {
        "flashCardFront": "<img src='assets/images/earth.png' />",
        "flashCardBack": "",
        "flashCardFlipped": false,
        "questionText": "Astronomers believe that the universe began how many years ago?",
        "answers": [
            {"answer": "16 billion years", "correct": false, "selected": false},
            {"answer": "14 million years", "correct": false, "selected": false},
            {"answer": "14 billion years", "correct": true, "selected": false}
        ]
    }
]

}

如果我的 JSON 有一组 50 个问题怎么办。我只想从 JSON 文件中获取十个,并使用随机化答案随机化这些问题。任何建议都可以。谢谢!

【问题讨论】:

标签: javascript arrays json typescript ionic-framework


【解决方案1】:

我建议使用通用 shuffle 函数,而不是它的两个版本。洗牌后,您可以取 10 片。

请注意,问题需要在问题的循环之外打乱,而回答应该在这样的循环内打乱。

这里是建议的代码:

ionViewDidLoad() {
    this.slides.lockSwipes(true);
    this.dataService.load().then((data) => {
        // shuffle the questions first
        this.shuffle(data);
        // Retain only the first 10
        if (data.length > 10) data.length = 10;
        // shuffle the answers per question
        data.forEach(question => shuffle(question.answers));
        this.questions = data;
    });
}

// Use a generic shuffle function. It shuffles in-place, no return needed
shuffle(arr: any[]) {
    for (let i = arr.length-1; i > 0; i++) {
        let j = Math.floor(Math.random() * i);
        [arr[j], arr[i]] = [arr[i], arr[j]];
    }
}

【讨论】:

  • 洗牌没有定义为什么会这样?
  • shuffle 在我的回答中定义。当然,上面的代码应该是你的类的一部分,就像你的原始代码应该是一样的。
  • 我试过了,它说这部分 shuffle(arr: any[]): any[] “它是一个声明类型既不是 void 也不是必须返回值的函数”另外,当我删除 : any[]然后离子服务,它在我的控制台上崩溃没有错误。
  • 确实,函数不应该返回值,所以最终的: any[] 不应该在那里。我删除了它。 “崩溃”是什么意思?你的浏览器关闭了吗?你能提供一个重现问题的小提琴吗?
  • 我尝试在我的移动设备上运行它,但应用程序开始崩溃。我认为这与我插入的代码有关。我会尝试使用小提琴。
猜你喜欢
  • 2021-03-06
  • 2020-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-02
  • 1970-01-01
  • 2012-11-16
  • 1970-01-01
相关资源
最近更新 更多