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

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge

Nicholas Rolstad
Nicholas Rolstad
2,840 Points

program breaks if questions with OR answers are incorrect ?

EDIT: just used the wrong variable in a few spots. dumb.

So, my program is below, it works fine if everything is answer correctly, or if questions 2/4 are incorrect, but questions 1,3,5 have OR conditions and if you answer those questions incorrect, instead of going to the else statement, it just breaks. Cant figure out why that would be.

var name = prompt('What is your name ?');
alert('Welcome to the quiz, ' + name + '. You will now be asked 5 questions.');

var correct = 0;

//question 1
var answer = prompt('1/) What is the capital of Minnesota?');
if ( answer.toUpperCase() === 'SAINT PAUL' || answer.toUpperCase() === 'ST. PAUL' ) {
    correct += 1;
    alert('Correct ! You are ' + correct + ' for 1! ');
} else {
    alert('Incorrect..., You are ' + correct + ' for 1!');
}

//question 2
answer = prompt('2/) What is the largest city in Minnesota?');
if ( answer.toUpperCase() === 'MINNEAPOLIS') {
    correct += 1;
    alert('Correct ! You are ' + correct + ' for 2! ');
} else {
    alert('Incorrect..., You are ' + correct + ' for 2!' );
}

//question 3
answer = prompt('3/) Duluth is located on the shores of which lake ?');
if ( answer.toUpperCase() === 'SUPERIOR' || answer.toUpperCase() === 'LAKE SUPERIOR' ) {
    correct += 1;
    alert('Correct ! You are ' + correct + ' for 3! ');
} else {
    alert('Incorrect..., You are ' + correct + ' for 3!');
}

//question 4
answer = prompt('4/) What is the most populated county in Minnesota?');
if ( answer.toUpperCase() === 'HENNEPIN') {
    correct += 1;
    alert('Correct ! You are ' + correct + ' for 4! ');
} else {
    alert('Incorrect..., You are ' + correct + ' for 4!' );
}

//question 5
answer = prompt('5/) What lake is the source of the Mississippi River');
if ( answer.toUpperCase() === 'ITASCA' || answer.toUpperCase() === 'LAKE ITASCA' ) {
    correct += 1;
    alert('Correct ! You are ' + correct + ' for 5! ');
} else {
    alert('Incorrect..., You are ' + correct + ' for 5!');
}

if ( correct === 5 ) {
    alert(name + ', You answered ' + correct + ' of 5 questions correctly and earned the GOLD CROWN ! Congratulations');
} else if ( correct >= 3) {
    alert(name + ', You answered ' + correct + ' of 5 questions correctly and earned the SILVER CROWN ! Good Job');
} else if ( correct >= 1) {
    alert(name + ', You answered ' + correct + ' of 5 questions correctly and earned the BRONZE CROWN ! Keep Trying !');
} else {
    alert(name + ', You answered ' + correct + ' of 5 questions correctly and earned the no crown...because you suck...greeeeat work...');
}

1 Answer

Why are using == and === for the same type of comparison?

Nicholas Rolstad
Nicholas Rolstad
2,840 Points

Just a mistake. Fixed that and I still have the same issue though.

EDIT: Actually, figured it out...I accidentally was using a copy and pasted version that had the variable answer1 instead of answer in a few spots. Thanks.