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

Complex 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

Your 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

This 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?

Nevermind. 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!

No problems ! ;) Next time you can try http://jsfiddle.net/ to test your code.

What 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."

Thanks for the link Jonathan!

Or just use chrome tools ... very quick

Chrome dev tools are great too, thanks Armand!