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

Hao Wu
2,394 PointsThe 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

deletedaccount
5,089 PointsIt 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>

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

Andrew Chalkley
Treehouse Guest TeacherThe 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.

ninyo
15,367 PointsMy 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
Treehouse Guest TeacherWhen 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?

Andrew Chalkley
Treehouse Guest Teacher+1 For Charles - thanks for chiming in.

Femke Boermans
11,240 PointsI tried that but then it says: Bummer! You didn't define the 'isValidEmail' function.

Andrew Chalkley
Treehouse Guest TeacherHi 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

Braden Ripple
Courses Plus Student 5,631 PointsHow come the argument is != -1 and not just == 1 or ===1?

Andrew Chalkley
Treehouse Guest TeacherBecause indexOf
doesn't return the number of occurrences but the actual index of the @
symbol.

Braden Ripple
Courses Plus Student 5,631 Pointsok, well why do they bother with the != -1 argument in the first place?

Braden Ripple
Courses Plus Student 5,631 Pointsthanks 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
1,591 PointsI was thinking about the same... Would be nice!

ninyo
15,367 Pointsif a small section was misunderstood, i usually review the whole video to better ground more foundation of a concept

Todd Johnson
3,605 PointsI finally solved it after many iterations...instead of $(email), I just used email. Can anyone explain why?
function isValidEmail(email) { return email.indexOf('@') != -1; }

Jinwoong Joung
3,132 PointsThnkx!

trishagawley
10,483 PointsHi, 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
Treehouse Guest TeacherYou 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?

trishagawley
10,483 PointsI figured it out!! :)