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
atesyanik
3,628 PointsProblem with My 5 Question Quiz solution
I am trying to finish the The Ultimate Quiz challenge but it seems like I am following a wrong path.
If all 5 questions answered correctly. totalCorrect variable value becomes 6 instead of 5. Can anyone help me solve this issue? If this way is totally wrong, please let me know.
Many thanks in advance.
var answer1 = prompt('Which country has Pisa tower?');
var answer2 = prompt('Which country has Goldengate Bridge');
var answer3 = prompt('Where is Eifel tower?');
var answer4 = prompt('Where is Big Ben tower?');
var answer5 = prompt('Which country has Galata tower?');
var userAnswer1 = 'Italy';
var userAnswer2 = 'United States';
var userAnswer3 = 'France';
var userAnswer4 = 'United Kingdom';
var userAnswer5 = 'Turkey';
var totalCorrect = 0;
if (userAnswer1 === answer1) {
var totalCorrect = 1
}else {
totalCorrect;
}
if (userAnswer2 === answer2) {
totalCorrect = totalCorrect + 1;
}else {
totalCorrect;
}
if (userAnswer3 === answer3) {
totalCorrect = totalCorrect + 1;
}else {
totalCorrect;
}
if (userAnswer4 === answer4) {
totalCorrect = totalCorrect + 1;
}else {
totalCorrect;
}
if (userAnswer4 === answer4) {
totalCorrect = totalCorrect + 1;
}else {
totalCorrect;
}
if (userAnswer5 === answer5) {
totalCorrect = totalCorrect + 1;
}else {
totalCorrect;
}
if (totalCorrect===1){
document.write('<p>You have one crown!</p>');
} else if (totalCorrect===2){
document.write('<p>You have 2 crowns!</p>');
} else if (totalCorrect===3){
document.write('<p>You have 3 crowns!</p>');
} else if (totalCorrect===4){
document.write('<p>You have 4 crowns!</p>');
} else if (totalCorrect===5){
document.write('<p>You have 5 crowns!</p>');
} else {
document.write('<p>Sorry. You have no crowns...</p>');
}
3 Answers
rydavim
18,814 PointsIt looks like you may have just copy-pasted your if-else loops one too many times. Looking at it, it looks like you have two sets for question/answer pair number four. If you remove one of those, you should end up with the correct final total.
It's really easy to miss stuff like that when you're looking at your own code, because we have a tendency to read it how we meant to write it. Happy coding!
Julian Gutierrez
19,325 PointsIt looks like you have the conditional for question 4 twice.
atesyanik
3,628 Pointsrydavim & Julian Gutierrez Thank you so much!!!!