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 trial
Hanzo Akimaru
161 PointsWhat's wrong with this code?
It's supposed to ask a series of questions which are stored in a 2D array, keep track of which questions were answered correctly/incorrectly, then print the results to the document. BTW, I'm new to JS so please don't be too harsh towards any n00by mistakes I've made.
var rightGuess=[];
var wrongGuess=[];
var quiz=[
['What is the name of The Fourth Hokage?', 'MINATO NAMIKAZE'],
['Who is the strongest member of The Uchiha Clan?', 'MADARA UCHIHA'],
['Who is the strongest memeber of The Senju Clan?', 'HASHIRAMA SENJU'],
['Who is The Mother of all Chakra?', 'KAGUYA OTSUTSUKI']
];
function print(message) {
var containerDiv=document.getElementById('div1');
containerDiv.innerHTML=message;
}
//checks to see if parameter is rightGuess or wrongGuess.
function tallyUp(par){
var test=par.indexOf(false);//if wrongGuess is passed then par must contain false value.
function logIt(){
var listHTML = '<ol>';
if (test<0) {
for (var x=0; x<(par.length-1); x+=1){
listHTML+='<li>'+par[x]+'</li>';
}
} else {
for (var x=0; x<par.length; x+=1){
listHTML+='<li>'+par[x]+'</li>';
}
}
listHTML+='</ol>';
print(listHTML);
}
if (test<0){
print('You got these questios right.');
logIt();
} else {
print('You got these questions wrong.');
logIt();
}
}
for (var x=0; x<quiz.length; x+=1) {
var guess = false;
for (var y=0; y<3; y+=1) {
var answer=prompt(quiz[x][0]);
if (answer.toUpperCase()===quiz[x][1]) {
alert('Correct');
rightGuess.push(quiz[x][0]);
guess=true;
break;
} else {
alert('Incorrect');
}
}
if (!guess) {
wrongGuess.push(quiz[x][0]);
}
}
wrongGuess.push(false);
tallyUp(rightGuess);
tallyUp(wrongGuess);
1 Answer
Hanzo Akimaru
161 PointsOliver Duncan Yes, I want to print a list of all correct answers and all incorrect answers. I push the false value because if the array which gets passed to the function contains that value then it must be the list of incorrect answers (since the correct answers list doesn't contain that value).
Oliver Duncan
16,642 PointsOliver Duncan
16,642 PointsI'm a little confused about what you're expecting from the script. Even if I get all the right answers, why do you push false into wrongGuess and print it to the screen? Are you trying to print all the correct guesses?