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
Brian OConnell
8,282 PointsComplex Conditions with Functions in Javascript
The title is probably off but, I thought it covered the topic.
I was playing around with javascript on my own and tried to write a function with complexed conditions.
Here is the code copied from Sublime Text 2
<!DOCTYPE html> <html> <head> </head> <body> <h1>JavaScript Test</h1>
<script type="text/javascript">
function joey(first, last){
if( (first == "Joey") && (last == "Two Toes") ){
document.write("Joey, how are ya?<br>");
}else if( (first == "Joey") || (last == "Two Toes") ){
document.write("You're almost Joey but, not quite.<br>");
}else {
document.write("Go find Joey.<br>");
}
}
joey("Joey", "Two Toes");
joey("Tommy", "Two Shoes");
joey("Tommy", "One Shoe");
</script> </body> </html>
The first call prints out the correct sentence but, the second and third calls both print out the result of the else condition. Anything would help.
7 Answers
Jonathan Dion
6,131 PointsYour code is good, I don't see the problem .
First condition print : Joey, how are ya? Second condition print : Go find Joey but If you replace last === "Two Toes" by last === "Two Shoes" its will print : You're almost Joey but, not quite. Third condition Print : Go find Joey
Brian OConnell
8,282 PointsThis is what I get when I drag and drop the .html file into Chrome
Joey, how are ya? Go find Joey. Go find Joey.
The else if condition doesn't seem to work. Is there a better option? Something better than if-else if-else?
Brian OConnell
8,282 PointsNevermind. I just saw that the second function took (Joey, Two Shoes) when I meant the parameters to be (Joey, Two Toes)
Just a typo on my part! Thanks for taking a look at the code and helping out!
Jonathan Dion
6,131 PointsNo problems ! ;) Next time you can try http://jsfiddle.net/ to test your code.
Jonathan Dion
6,131 PointsWhat are you expecting? the else if statement is good because "Tommy" is not equal to "Joey" or "Two Toes" is not equal to "Two Shoes" what why it's print "Go find Joey."
Brian OConnell
8,282 PointsThanks for the link Jonathan!
Armand Ramirez
4,016 PointsOr just use chrome tools ... very quick
Brian OConnell
8,282 PointsChrome dev tools are great too, thanks Armand!