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 Solution

Kayleen Carido
Kayleen Carido
1,048 Points

Little syntax difference with .toLowerCase()

Hey all,

When writing the code for this challenge, I found that changing the user's answer to all lower case to avoid errors with case sensitivity gave me a few problems. Here is a snippet of code:

if (q1.toLowerCase() === "red") {
score += 1;
}

At first, I was writing it without the parentheses following .toLowerCase which resulted in errors. After some time messing around with it and using console.log to see what happens with the input, including those parentheses seemed to do the trick.

When I watch the video, he doesn't include parentheses when using the .toUpperCase property, but it still executes. I'm a little confused as to why this is, and maybe clarification on when it is appropriate to include these parentheses would be greatly appreciated.

If it helps, I'm using Brackets to write the code and Chrome to live preview the work. Sometimes launching Treehouse's Workspaces doesn't load.

3 Answers

Hi Kayleen,

I think the parentheses were an accidental omission from the videos. If you check the video around 1:00, you'll see that Dave uses them for the .toUpperCase() method for the first answer. The rest of the answers are missing it.

You mentioned that the code still executes but I don't see where in the video Dave runs the code. I don't believe it would run without errors.

You'll want to put parentheses after a function name anytime you want the function to be called and executed right then and there. In this case, you want the toLowerCase method to be called right at that moment so that it can immediately lowercase your q1 variable which in turn can be compared to "red"

If you leave the parentheses off, then it won't be called and you simply have a reference to the function. You'll see examples of this if you progress further with the javascript courses.

Kayleen Carido
Kayleen Carido
1,048 Points

Hi Jason,

Whoops! Looks like Dave never ran the code after typing it out for us. Might've mixed that up in my mind when I was stuck trying to run it on my end, my bad.

Thanks for the clarification on what happens when parentheses are missing. Moving forward into the functions portion of this course, it's making a lot more sense as to why that is.

Cheers!

Steven Parker
Steven Parker
231,275 Points

The parentheses are necessary to call the method.

Without the parentheses, you are referring to the method itself and not the result of calling it.

If you notice at the beginning of the video, while dealing with answer1, the parentheses are included. This is the correct usage. I never noticed until you pointed it out that for the other answers the parentheses are omitted. This is not correct, and that code will not run!

:beetle: It seems that you've discovered a bug in the video. You may want to report this to Support, it might get you a "special Exterminator badge".

Kayleen Carido
Kayleen Carido
1,048 Points

Steven,

Thanks for the response! Definitely took your word and sent some feedback over to Support. (:

Steven Parker
Steven Parker
231,275 Points

FYI: You can create an actual smiley face like this :point_right: :smile: :smile:

Meghan Lewis
Meghan Lewis
1,697 Points

I noticed that as well and was curious. I also noticed he forgot to close out a <p> tag as well with a </p> at one point.

HIs solution ended up being way more easy than mine. I had to use parseInt(); because I did a math quiz. (Why I did that to myself? I don't know.)

When you're on a roll...