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

conditional challenge

I am on the conditional challenge. I can get the dialog box to show the first question, but when I ask the second, something seems to go wrong. Can you tell me what it is? Why does the second question being asked not allow the first question to be asked?

var correct = 0; //ask questions var president = prompt("What is the name of the president?"); if (president.toUpperCase() === 'OBAMA') { document.write ("That is correct."); }else{ document.write("You are a moron."); correct +=1; } var vicepresident = prompt("What is the name of the vice president?"); if (vicepresident.toUpperCase === 'BIDEN'){ document.write (That is correct."); }else{ document.write("You should shoot yourself in the head; do it now!");
correct +=1; }

Darren Joy
Darren Joy
19,573 Points

Just throwing the code down in a Markdown fashion

var correct = 0; //ask questions 
var president = prompt("What is the name of the president?"); 
if (president.toUpperCase() === 'OBAMA') { 
    document.write ("That is correct."); 
    }else{ 
    document.write("You are a moron."); 
    correct +=1; } 
var vicepresident = prompt("What is the name of the vice president?"); 
if (vicepresident.toUpperCase === 'BIDEN'){ 
    document.write (That is correct."); 
    }else{ 
    document.write("You should shoot yourself in the head; do it now!");
    correct +=1; }

3 Answers

Hey Matt,

There are a few problems with your code. You were missing the beginning " string character right after your if statement for the vice president inside the document.write() command. Also, you were missing () after toUpperCase in your vice president code. There is also a logic error which is that you are adding + 1 to the correct variable for an incorrect response. Correct += 1 belongs in the if statement itself.

var correct = 0; //ask questions 
var president = prompt("What is the name of the president?"); 
if (president.toUpperCase() === 'OBAMA') { 
    document.write("That is correct."); 
    correct += 1;
}
else { 
    document.write("You are a moron."); 
} 
var vicepresident = prompt("What is the name of the vice president?"); 
//toUpperCase() is a method and requires () to be called
if (vicepresident.toUpperCase() === 'BIDEN') { 
    //needed beginning string character
    document.write("That is correct."); 
    correct += 1; 
} 
else { 
    document.write("You should shoot yourself in the head; do it now!");
}
Darren Joy
Darren Joy
19,573 Points

OK, I ran it through WorkSpaces then looked at the Console to troubleshoot..

got this error: Uncaught SyntaxError: missing ) after argument list

also said: scripts.js:15

That means line 15 is likely missing an ), which often means one got left out much earlier and it's counting them all offsent from one another

This is line 15: document.write (That is correct."); Which kinda looks correct so if I go up a line: if (vicepresident.toUpperCase === 'BIDEN'){ I notice that .toUpperCase is missing the usual () after it.

I threw all this info down to give you an idea of the process of troubleshooting this kind of thing

Darren Joy
Darren Joy
19,573 Points

and also second run through as Marcus Parsons mentions its missing an opening "

I've noticed that JavaScript errors are usually not helpful at all, because even though the error says that that it came from from a missing parenthesis, it was the quote that was causing the error. The code will run without using () on toUpperCase; however, the toUpperCase method itself will not work, and you'll get a wrong answer unless you answer with "BIDEN".

Darren Joy
Darren Joy
19,573 Points

Also, you will shortly run into a bunch of vids on using the console to troubleshoot, but here's another bit of good info:

http://blog.teamtreehouse.com/mastering-developer-tools-console