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

Jenny Swift
Jenny Swift
21,999 Points

checking values code challenge task one

Hi, I know the correct answer for this code challenge from searching the forum, but I don't understand why it is the correct answer and what is wrong with my attempt.

The question is: Create a method called 'isValidEmail' that takes one argument that returns true when a string with an '@' symbol is passed, false if not.

The correct answer is:

function isValidEmail (email) {
     return email.indexOf("@") != -1;
} 

If I was to use my own logic rather than copying and pasting the correct answer, this is what I would end up with:

function isValidEmail (email) {
    if (email.indexOf("@") != -1;) {
     return true;
     } else {
     return false;
     }
}

Can anyone please explain to me what is wrong with my logic here? I don't understand how the correct answer can be correct without using return true, return false, and if and else.

1 Answer

Hi Jenny, i think both functions will end up in the same result, the only thing that changes is the number of lines you use for achieving the test.

If you think about it, when javascript test <email.indexOf("@") != -1;> the possible values that we will have are a -1 which is considered false, or a positive number which is considered true.

In the other case, the code will first test the expression with the if statement (the if statement tests either a true or a false value) and then if it is true then you explicitly are returning a true, if not then explicitly you return a false value.

I hope that will answer your question.

Jenny Swift
Jenny Swift
21,999 Points

Thanks Juan. However I still don't understand why I get 'Bummer! You didn't define the 'isValidEmail' function' when I type in the second bit of code above. I figure that means something is wrong with my code.