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 trialThomas Faller
9,640 PointsMy correct answers are not adding up?
I have the intuition the values are not adding up as they might need to be converted to Integrals?
// Use conditional statements to build a multiple question quiz.
// Reseting the number of correct answers.
var correctAnswers = 0;
// Questions 1-5
var answer1 = prompt("How much is 1+1?");
if (answer1 === 1) {
correctAnswers += 1;
}
var answer2 = prompt("How much is 2+2?");
if (answer2 === 4) {
correctAnswers += 1;
}
var answer3 = prompt("How much is 4+4?");
if (answer3 === 8) {
correctAnswers += 1;
}
var answer4 = prompt("How much is 8+8?");
if (answer4 === 16) {
correctAnswers += 1;
}
var answer5 = prompt("How much is 16+16?");
if (answer5 === 32) {
correctAnswers += 1;
}
// How many correct answers in total?
alert("You've got " + correctAnswers + " correct answers!");
if (correctAnswers > 4) {
alert("That's brilliant! Well done!");
} else if (correctAnswers > 3) {
alert("That's great!");
} else if (correctAnswers > 2) {
alert("Not too bad");
} else if (correctAnswers > 1) {
alert("Could have been better");
} else if (correctAnswers > 1) {
alert("At least you got 1 right!");
} else {
alert("0?!!!");
}
2 Answers
Sam Baines
4,315 PointsHi Thomas - first thing that might need pointing out for you to check.
When your user inputs the answer to a question I believe it returns a string so "1" instead of the integer 1 - so you might need to convert the string to an integer - look at the parseInt() function.
Hope that helps.
Burton King
3,938 PointsThis part is adding up wrong.... var answer1 = prompt("How much is 1+1?"); if (answer1 === 1) {
answer ===2 instead
Thomas Faller
9,640 PointsOh indeed :) Thanks Burton!
Thomas Faller
9,640 PointsThomas Faller
9,640 PointsThanks Sam,
Indeed, I wasn't aware that JS is actually taking anything that's "prompted" as a string value by default. Since my function is relying on addition, it makes sense that I need those to be integers.
Thanks again! :)