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
Michael Beers
5,810 PointsThe Conditional Challenge
I am having an issue with my code. My correct answers are not being counted . I am also thinking that my my answers in my if statements are not set up quite right. Wondering if someone can push me in the right direction.
var answersCorrect = 0;
var question1 = prompt("What is 5 + 5?");
if (question1 === parseInt(10)) {
answersCorrect += answersCorrect;
}
console.log (answersCorrect); \\testing if correct answer gets loged
var question2 = prompt("What is the month?");
if (question2 === "NOVEMBER".toUpperCase()) {
answersCorrect += answersCorrect;
}
var question3 = prompt("How many licks does it take to get to the center of a tootie pop?");
if (question3 === parseInt(2)) {
answersCorrect += answersCorrect;
}
var question4 = prompt("How many sides are there in a cube? ");
if (question4 === parseInt(6) ) {
answersCorrect += answersCorrect;
}
var question5 = prompt("What color is the sun?");
if (question5 === "YELLOW".toUpperCase()) {
answersCorrect += answersCorrect;
}
if (answersCorrect === 5) {
document.write("You get a gold crown!!!");
} else if (answersCorrect === 4) {
document.write("You get a silver crown!!!");
}else if (answersCorrect === 3 || answersCorrect === 2 || answersCorrect === 1) {
document.write("You get a bronze crown!!!");
}else {
document.write("You missed them all. No crown for you.");
}
Jason Anello
Courses Plus Student 94,610 PointsI fixed your code formatting for you.
Please see this thread: https://teamtreehouse.com/forum/posting-code-to-the-forum
3 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Michael,
As you suspected, there is a problem with your conditionals.
This is your first conditional: question1 === parseInt(10)
The problem is that you're calling parseInt on the number 10 which remains unchanged. It comes back out as the number 10. And you're comparing that against whatever string the user entered. It's the user input string that needs to be parsed to an int and then compared to the number 10.
Similar thing to the string related conditionals like question2 === "NOVEMBER".toUpperCase()
You're trying to upper case an already uppercased string which doesn't change it and then comparing it to whatever the user input was, which could have been all lower case. You want to upper case the user input and then compare to "NOVEMBER".
Another problem is how you're incrementing the correct answers. You're adding on the current value of answersCorrect which starts off at zero. This means you keep adding zero and it never changes.
When you get a correct answer the number should go up by one. So you only want to increment by 1 each time.
Also, if you're following the challenge instructions strictly then you should be testing for 3 and 4 for the silver crown.
Give us an update if it's still not working after those fixes.
Michael Jurgensen
8,341 PointsEDIT: Modified the code based on Jason's suggestion.
Jason covered the reasoning. Here is the fixed code.
var answersCorrect = 0;
var question1 = prompt("What is 5 + 5?");
if (parseInt(question1,10) === 10)
{
answersCorrect++;
}
alert(answersCorrect); //testing if correct answer gets loged
var question2 = prompt("What is the month?");
if (question2.toUpperCase() === "NOVEMBER")
{
answersCorrect++;
}
alert(answersCorrect);
var question3 = prompt("How many licks does it take to get to the center of a tootie pop?");
if (parseInt(question3,10) === 2)
{
answersCorrect++;
}
alert(answersCorrect);
var question4 = prompt("How many sides are there in a cube? ");
if (parseInt(question4,10) === 6)
{
answersCorrect++;
}
alert(answersCorrect);
var question5 = prompt("What color is the sun?");
if (question5.toUpperCase() === "YELLOW")
{
answersCorrect++;
}
alert(answersCorrect);
if (answersCorrect === 5) {
document.write("You get a gold crown!!!");
} else if (answersCorrect === 4) {
document.write("You get a silver crown!!!");
}else if (answersCorrect === 3 || answersCorrect === 2 || answersCorrect === 1) {
document.write("You get a bronze crown!!!");
}else {
document.write("You missed them all. No crown for you.");
}
Jason Anello
Courses Plus Student 94,610 PointsIt would probably be better to do the upper-casing and parseInt inside the conditional like Michael B. had it except on the user input.
It would save you some lines of code and you would still have original user input in those variables. This would be good if you wanted to print a summary of the results. You likely would want to output what the user actually typed in.
Michael Beers
5,810 PointsThanks for your input. After reading your answer it makes sense why my code was not working. I will make my adjustments. Thanks again
haunguyen
14,985 Pointshaunguyen
14,985 Pointsmaybe answersCorrect += 1;