Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDongyun Cho
2,256 PointsMy Solution unilt now
This is what I did til now as long as I know, I'll keep making it better after watching next videos. What I'm thinking is that, I'd like to use Math.floor( Math.random() * 2) this code for changing the order of two guesses, but I don't know how to do this. Can anyone help me?
quiz.js
function Quizlist(){
this.quizes = [];
this.score = 0;
this.nowIndex = 0;
}
Quizlist.prototype.add = function(quiz){
this.quizes.push(quiz);
};
var question = document.getElementById('question');
var choice0 = document.getElementById('choice0');
var choice1 = document.getElementById('choice1');
var progress = document.getElementById('progress');
Quizlist.prototype.renderInElement = function(){
question.innerHTML = this.quizes[this.nowIndex].text;
choice0.innerHTML = this.quizes[this.nowIndex].guessTrue;
choice1.innerHTML = this.quizes[this.nowIndex].guessFalse;
progress.innerHTML = 'Score : ' + this.score + ' / ' + this.quizes.length;
}
question.js
function Question(text, guessTrue, guessFalse){
this.text=text;
this.guessTrue = guessTrue;
this.guessFalse = guessFalse;
}
app.js
var quizlist = new Quizlist();
var firstQuestion = new Question("what is right one", "True", "False");
var secondQuestion = new Question("what is the fruit I like" , "Apple", "Peach");
var thirdQuestion = new Question("what is what", "YEAH", "No");
quizlist.add(firstQuestion);
quizlist.add(secondQuestion);
quizlist.add(thirdQuestion);
quizlist.renderInElement();
var guess0 = document.getElementById('guess0');
var guess1 = document.getElementById('guess1');
guess0.onclick= function(){
quizlist.score ++;
quizlist.nowIndex ++;
quizlist.renderInElement();
if(quizlist.nowIndex==quizlist.quizes.length){
alert(quizlist.score);
}
}
guess1.onclick= function(){
quizlist.nowIndex ++;
quizlist.renderInElement();
if(quizlist.nowIndex==quizlist.quizes.length){
alert(quizlist.score);
}
}
1 Answer
Amber Cyr
19,699 PointsIn the question.js file you could do something like this:
//Durstenfeld shuffle algorithm
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
function Question(text, guess1, guess2){
var guesses = shuffleArray([guessTrue, guessFalse]);
this.text=text;
this.guess1 = guesses[0];
this.guess2 = guesses[1];
}