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
Evan Bean
Courses Plus Student 4,145 PointsWhy did I need extra parentheses to make the .toUpperCase() method work?
I got my quiz program to function and it even matched Dave's solution, minus variable names and such, pretty closely. However, I had trouble getting my comparisons to work. It kept resulting in "incorrect" answers (even when they were correct) until I added a second set of parentheses around the variable and method, like this: (firstQ.toUpperCase()). When I only had firstQ.toUpperCase(), the conditional always went to the else clause.
Did I do something wrong so that I needed to add those extra parentheses?
Including one of my question code blocks as a sample:
// First Question var firstQ = prompt('What is the capital of Denmark?'); if ((firstQ.toUpperCase()) === 'COPENHAGEN') { questionsLeft -= 1 alert('That\'s right! ' + questionsLeft + ' questions left!'); answer += 1; } else { questionsLeft -= 1 alert('Sorry, that\'s wrong. ' + questionsLeft + ' questions left!'); }
1 Answer
Abraham Juliot
47,353 PointsHi Evan,
In this case, no. You are fine without the extra parenthesis. Although, using parenthesis can provide readibility for a group of expressions, it's only necessary to specify operator precedence.
In your code, you may need to include semicolons after both instances of questionsLeft -= 1 .
Evan Bean
Courses Plus Student 4,145 PointsEvan Bean
Courses Plus Student 4,145 PointsThanks, Abraham. The semicolons were the missing factor. I missed them when I wrote the code block for the first question and repeated the error by copying the block for each of the following questions.
Yes, I know I should have been fine without the extra parentheses. I didn't ask if I needed them. I asked why. As I said in my original post, I had to add those to make the program run properly, and I didn't know why. Now I know it was those missing semicolons.
Thanks for the help.