2017-08-29 42 views
0

我按照网上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文件中获得十个,并随机化这些问题和随机答案。任何建议都可以。谢谢!

+0

我建议你重写了这个问题。你没有描述你遇到麻烦的地方。请看这个:https://stackoverflow.com/help/how-to-ask – Kamran

回答

0

我会建议使用通用的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]]; 
    } 
} 
+0

洗牌没有定义为什么? – AngularNewbie

+0

'shuffle'在我的回答中定义。当然,上面的代码应该是你的类的一部分,就像你原来的代码应该是。 – trincot

+0

我试过了,它说这部分'shuffle(arr:any []):any []'这是一个函数,它的声明类型既不是空也不必返回值' 另外,当我删除了':any []'then ionic serve,它在我的控制台上崩溃没有错误 – AngularNewbie