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
Nathan Marshall
6,031 PointsCould someone please explain how this works? Shouldn't one outcome run at a time?
Hi Guys, I'm running through the Javascript Basics Course I have reached conditional statements and was wondering why my answer is still correct if I typed for example"JavascriPT".
I am trying to cover all bases for the answer using else if statements so that it doesn't matter what case it is in (Sentence Case / Mixed Case etc) but I feel that the answer of "JavascriPT" should not work? As only one outcome can run at a time?
Could anyone please explain why this answer works?
Here is my code:
if ( answer1 === "JavaScript") { document.write("<h2>You got it correct! Welldone</h2>"); } else if ( answer1.toUpperCase() === "JAVASCRIPT") { document.write("<h2>You got it correct! Welldone</h2>"); } else if ( answer1.toLowerCase() === "javascript") { document.write("<h2>You got it correct! Welldone</h2>"); }
else { document.write("<h2>Wrong</h2>"); }
3 Answers
Tsenko Aleksiev
3,819 PointsWhy shouldn’t it work?
if JavascriPT === JavaScript //its not
...
else if JavascriPT.toUpperCase() aka JAVASCRIPT === JAVASCRIPT, well yes it is
...
else if JavascriPT.toLowerCase() aka javascript === javascript, well yes it is.
...
Do you get the idea? You use both methods on the input and each one makes the word entered upper or lower case, no matter how it is entered. That’s way no matter how the user has typed javascript, with your code it will always work :) My suggestion is to do it like this:
if(answer1.toUpperCase() === "JAVASCRIPT"){ //or toLowerCase() === "javascript" doesn't matter
document.write("<h2>You got it correct! Welldone</h2>");
}else {
document.write("<h2>Wrong</h2>");
}
I think you don't have to do all those checks. Your firstone checks if the user input is exactly the same as you think ("JavaScript") and after that you tell JS to make the user input all upper case and check if it is the same as "JAVASCRIPT". Same as user input all lower case and check if it's the same as "javascript". You need only one check to be sure that the user has entered javascript no matter JaVaScRiPt or something else. Did you get what I mean, I'm not sure how I explained it. :)
Antti Lylander
9,686 PointsOpen console in your browser (in chrome ctrl+shift+i) and type these in:
const answer1 = "JavascriPT"; answer1.toUpperCase(); answer1.toLowerCase();
See how it works? toUpperCase() changes every letter to upper case. No matter if the original was in lower or upper case.
Nathan Marshall
6,031 PointsI understand now thank you Antti! :D
Tsenko Aleksiev
3,819 PointsExactly! :) lol I explained it wright hah. That’s the idea you convert it anyway
Nathan Marshall
6,031 PointsNathan Marshall
6,031 PointsYeah I think so. So basically regardless of what is typed it basically gets converted anyway?
Looking back I think I’ve over complicated things in my head.
Thanks Tsenko!