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

The problem of defining function. (Challenge Task Problem)

I encounter a problem of defining function.

The problem comes from the link below: http://teamtreehouse.com/library/checking-values

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

Here is my answer:

            <script type="text/javascript" src="jquery.js">
              function isValidEmail(email){
                return $(email).indexOf("@") != -1;
              }  
            </script>

But I get a warning: You didn't define the 'isValidEmail' function.

15 Answers

It may have something to do with the fact that you're specifying src="jquery.js" and then go on to write code within the script tags.

Shouldn't you have separate jQuery link and custom script? Like this:

<script src="jquery.js"></script>
<script>
          function isValidEmail(email){
            return $(email).indexOf("@") != -1;
          }  
        </script>

Hey Femke, Instead of $(email).indexOf("@") try email.indexOf("@") [without dollar sign and brackets] Good Luck :P

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

The function is to test if the email is valid. So having the method return true or false than either a -1 or a number between 0...however long the string is.

The !=-1 will ensure that the function returns a boolean over a meaningless integer.

My question here would be what is actually the type that is passed into the parameters in the method isValidEmail()? is the parameter inside the parenthesis a string? because isValidEmail(string) with the return value of string.indexOf("@") != -1passes valid as well as isValidEmail(email) or whatever is passed into the parameters. Since this is so, it makes me think that any sort of word could be passed in the parameters, but if that's true, don't all string references need "quotes" to be referenced? Sorry just wanted some clarification on the language. Any sort of answer to this question will help a lot. Thanks!

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

When you create a function a parameter or variable can be given. In this case email. You could call it whatever you want as long as you use that name in your code. Also how this variable is used assumes it's a string.

Here's an example.

function say(word) {
    alert(word);
}

say("Hello");
say('Bye');

The above code would do an alert box saying Hello and Bye. I can alter the function like this:

function say(aWord) {
    alert(aWord);
}

say("Hello");
say('Bye');

And it would do the same thing.

Does that answer your question?

Femke Boermans
Femke Boermans
11,240 Points

I tried that but then it says: Bummer! You didn't define the 'isValidEmail' function.

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi Femke,

Showing your exact code in the context it's written in helps diagnose the problem you're having. You may have a character missing in your code causing a syntax error making the function undefined.

Regards Andrew

How come the argument is != -1 and not just == 1 or ===1?

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Because indexOf doesn't return the number of occurrences but the actual index of the @ symbol.

ok, well why do they bother with the != -1 argument in the first place?

thanks andrew, you know what would be cool? is if along with with the transcribed video you could make it so that you could click to that point of the video by click on the word or the line where that text is in the video timewise.

Alice Mello
Alice Mello
1,591 Points

I was thinking about the same... Would be nice!

if a small section was misunderstood, i usually review the whole video to better ground more foundation of a concept

I finally solved it after many iterations...instead of $(email), I just used email. Can anyone explain why?

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

Hi, I'm still stumped on this one... per Andrew's reply I changed the -1 to "true". It still says that I didnt' define the function 'isValidEmail'... please help, Thanks!

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

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

You do need to keep the -1, however if it's saying that isValidemail is undefined, what code do you have before it. There may be a syntax error. Can you share your code in full?

I figured it out!! :)