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

How to I use .toUpperCase

This is my code: My trouble is that I've tried many different methods for the .toUpperCase option for each answer, but I can't seem to get it to function. What is my problem.

//Questions var question1 = prompt("What do babies wear?"); var question2 = prompt("What do people wear on their legs?"); var question3 = prompt("What do people wear on their torso?"); var question4 = prompt("What do people wear on their head?"); var question5 = prompt("What do people wear on their hands?");

//if else statements

if( question1 === "diaper"){ var answerOne = 1 } else if( question1 ==="diapers" ){ var answerOne = 1 } else { var answerOne = 0; }

if( question2 === "pants" ){ var answerTwo = 1

} else if( question2 === "pant" ) { var answerTwo = 1 }else { var answerTwo = 0; }

if( question3 === "shirt" ){ var answerThree = 1

} else if( question3 === "shirts" ){ var answerThree = 1 } else { var answerThree = 0; }

if( question4 === "hat"){ var answerFour = 1 } else if( question4 === "hats"){ ( question4 ==="hats") } else { var answerFour = 0; }

if( question5 === "gloves"){ var answerFive = 1

} else if( question5 === "glove" ){ var answerFive = 1 } else { var answerFive = 0; }

var finalScore = parseInt(answerOne) + parseInt(answerTwo) + parseInt(answerThree) + parseInt(answerFour) + parseInt(answerFive);

if(finalScore >= 1 ){ alert("Congrats! Your final score is " + finalScore + "!"); } else{ alert("Please try again!"); }

//Type of metal won

if( finalScore > 4 ){ alert( 'You\'ve won the gold metal!'); } else if( finalScore >= 2){ alert('You\'ve won the silver metal!'); } else if( finalScore === 1){ alert('You\ve won the bronze metal!'); } else { alert('Thanks for playing!'); }

Steven Parker
Steven Parker
243,656 Points

For readability, when posting code please use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:

2 Answers

Steven Parker
Steven Parker
243,656 Points

You don't seem to be using it in this code.

So I'm guessing, but did you remember to put the parentheses after the method name? (".toUpperCase()") ? Also, since you seem to be comparing your answers to lower-case strings, you might want to use toLowerCase instead. Here's an example of one way you might use it:

var question1 = prompt("What do babies wear?").toLowerCase();

Also, be careful you aren't declaring variables more than once. You might also want to declare global variables before and outside of any conditional code blocks they are used in. And one more hint: variables that are assigned with numbers in the code don't need to be converted with parseInt.

Steven Parker
Steven Parker
243,656 Points

Regarding declaring global variables just one time: For example, instead of this:

if (question4 === "hat") {
    var answerFour = 1;
} else {
    var answerFour = 0;
} 

You might do this instead:

var answerFour;            // this declares the variable but doesn't assign it
// then sometime later...
if (question4 === "hat") {
    answerFour = 1;        // now it just gets assigned
} else {
    answerFour = 0;        // same here
} 

Thanks a bunch, was it not working because I declared variables three times? I did at one point have the .toLowerCase() in the correct spot, so it must have been?
It works without it, but what do you mean to declare a global variable?

Steven Parker
Steven Parker
243,656 Points

I added a comment to my answer.