我按照网上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文件中获得十个,并随机化这些问题和随机答案。任何建议都可以。谢谢!
我建议你重写了这个问题。你没有描述你遇到麻烦的地方。请看这个:https://stackoverflow.com/help/how-to-ask – Kamran