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 trialHunter Shaw
2,187 PointsMy code isn't working, help?
var questionsCorrect = 0;
var totalQuestions = 5;
// Question 1
var question_1 = prompt("What color is an apple?");
if ( question_1 === 'red' ) {
questionsCorrect = questionsCorrect + 1;
}
// Question 2
var question_2 = prompt("What is 10 + 15?");
if ( parseInt(question_2) === 25 ) {
questionsCorrect = questionsCorrect + 1;
}
// Question 3
var question_3 = prompt("How many days are in a week?");
if ( parseInt(question_3) === 7 ) {
questionsCorrect = questionsCorrect + 1;
}
// Question 4
var question_4 = prompt("What's the best programming language?");
if ( question_4 === 'javascript' ) {
questionsCorrect = questionsCorrect + 1;
}
// Question 5
var question_5 = prompt("How do you say hello in Spanish?");
if ( question_5 === 'hola' ) {
questionsCorrect = questionsCorrect + 1;
}
// How many right
document.write("You answered " + questionsCorrect + " correctly out of " + totalQuestions + "." + "<br>");
// if else statements
if ( questionsCorrect === totalQuestions ) {
document.write("You've earned the gold crown!");
}
else if ( questionsCorrect === 3 || questionsCorrect === 4 ) {
document.write("You've earned the silver crown!");
if ( questionsCorrect === 1 || questionsCorrect === 2 ) {
document.write("You've earned the bronze crown!");
}
} else {
document.write("You've earned no crown at all."
}
/*
What color is an apple?
What is 10 + 15?
How many days are in a week?
What's the best programming language?
How do you say hello in Spanish?
var answer_1 = 'red'; var answer_2 = 25; var answer_3 = 7; var answer_4 = 'javascript'; var answer_5 = 'hola'
// not sure why my code doesn't work, might be something to do with the if else statements at the end.
*/
2 Answers
Steven Parker
231,271 PointsIt looks like the line with "no crown at all" is missing a closing parenthesis (and probably a semicolon).
In future, when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. Or watch this video on code formatting.
Hunter Shaw
2,187 PointsHere's my code that is fixed, I fixed a few things from the code above.
-- Thanks :)
var questionsCorrect = 0;
var totalQuestions = 5;
// Question 1
var question_1 = prompt("What color is an apple?");
if ( question_1 === 'red' ) {
questionsCorrect = questionsCorrect + 1;
}
// Question 2
var question_2 = prompt("What is 10 + 15?");
if ( parseInt(question_2) === 25 ) {
questionsCorrect = questionsCorrect + 1;
}
// Question 3
var question_3 = prompt("How many days are in a week?");
if ( parseInt(question_3) === 7 ) {
questionsCorrect = questionsCorrect + 1;
}
// Question 4
var question_4 = prompt("What's the best programming language?");
if ( question_4 === 'javascript' ) {
questionsCorrect = questionsCorrect + 1;
}
// Question 5
var question_5 = prompt("How do you say hello in Spanish?");
if ( question_5 === 'hola' ) {
questionsCorrect = questionsCorrect + 1;
}
// How many right
document.write("You answered " + questionsCorrect + " correctly out of " + totalQuestions + "." + "<br>");
if ( questionsCorrect === totalQuestions ) {
document.write("You've earned the gold crown!");
}
else if ( questionsCorrect === 3 || questionsCorrect === 4 ) {
document.write("You've earned the silver crown!");
} else if ( questionsCorrect === 1 || questionsCorrect === 2 ) {
document.write("You've earned the bronze crown!");
} else {
document.write("You've earned no crown at all.");
}
/*
What color is an apple?
What is 10 + 15?
How many days are in a week?
What's the best programming language?
How do you say hello in Spanish?
var answer_1 = 'red';
var answer_2 = 25;
var answer_3 = 7;
var answer_4 = 'javascript';
var answer_5 = 'hola'
*/
Hunter Shaw
2,187 PointsHunter Shaw
2,187 PointsI can't believe I missed that. It just goes to show one one little thing can mess up your whole program. However, it did work and I really appreciate your suggestions.