@Jester12 wrote:
I have followed a tutorial on the internet [for my Quiz.][1]
I wanted to randomize the questions and only get 10 from my questions.json file. What am I doing wrong?
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; }
An example of the JSON I used which I followed from the tutorial:
{ "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} ] } ]
}
What if I have a set of 50 questions on my JSON. I want to get only ten from the JSON file and randomize those questions with the randomize answers. Any advice will do. Thanks!
[1]: https://www.joshmorony.com/create-a-data-driven-quiz-app-in-ionic-2-part-2/
Posts: 2
Participants: 2