Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
function myChildren(name){
if (name = "Dave")
return "Oldest"
if (name = "Michelle")
return "Middle";
}

else {
return "Not my kid!";
}
Posted
Comments
Richard Deeming 14-Dec-15 14:41pm    
You're missing a semi-colon on the return "Oldest" line, but Javascript is relatively lax about that.

You're also missing a closing brace (}) on the else block, and the opening brace ({) on the preceding if block.

You don't really need the else {, since your previous if block has an unconditional return.

Looks to me like your else block is OUTSIDE of your function.

That and the missing semicolon noted above by RD.
 
Share this answer
 
I think this code block should better read:
JavaScript
function myChildren(name) {
   if (name == "Dave") {
      return "Oldest";
   }
   else if (name == "Michelle") {
      return "Middle";
   }
   else {
      return "Not my kid!";
   }
} 

Identing your code correctly is the first thing to do when you want to quickly detect misplaced braces.
You should also try to be consistent with the way you are using if blocks. For the two first ones, you do not use braces, whereas you use them for the else part.
I also corrected your equality tests; as far as I know, conditional operator in javascript is ==, not =. = is the assignment operator.

Hope this helps.
 
Share this answer
 
Comments
W Balboos, GHB 15-Dec-15 12:33pm    
Just a trivial comment: since each member of the conditional contains a return statement there's no need for any "else if". Just the final "else" last one as a catch-all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900