JavaScript help

JavaScript help


Hey everyone! I’m working on a project with these instructions:

Create a function called whichChild that takes one paramater: a child’s name.
When passed in ‘Dave’ return 'Oldest’
When passed in ‘Michelle’ return 'Middle’
When given any other value return 'Not my kid!"

Here’s my code for it:
var whichChild = function (name) {
return "Dave"
return “Michelle”

if (whichChild = “Dave”)
if (whichChild = “Michelle”)
console.log(“Not my kid!”)

code pen doesn’t exactly tell me


Hey Andrew,

Here’s the code you’re looking for:

function whichChild(name) {
  if (name == "Dave") {
    return "Oldest";
  } else if (name == "Michelle") {
    return "Middle";
  } else {
    return "Not my kid!";


Some things to note:

  1. If you put “return” at the top before any other instruction, it will always return that value. Make sure that you do something before calling a return.

  2. “whichChild” is the name of the function. What you want to compare is the parameter value “name”.

  3. Don’t forget that “=” is used to assign a value to a variable. When you are comparing two values like you’re doing here, you need to use “==” or “===”.


Thanks for the input! =) next challenge is to create a function called sum that returns the sum of two numbers passed in as parameters, here’s the code I have

var sum = function(number) {
return number + number;


any advice?


I think it’s quite important that you make a distinction between an assignement and a comparison.

if (whichChild = “Dave”)

is an assignement and in this case overwrite the whichChild function with the string value “Dave”.

When comparing values, you always want to use the triple equals :

if (whichChild === “Dave”) …

Why ? Because two == doesn’t care for the value type, and if it matters to your application, it could lead to potential problems.

triple equals check if the right value type matches the left value, and so you are sure of what you’re getting !


var sum = function(number) {
return number + number;


sum(2,3) has two arguments, but your function definition only accepts one…


Meaning I need to set multiple parameters?


Correct! Set as much as you need to : function(number1, number2)


Worked! thank you =)


Thanks for the comments Mizu.

Actually, I did, in (3). :slight_smile:

That’s true, and it’s good advice! I understand the difference. In this particular example, the parameter is a string and the argument is being compared to a string so it doesn’t matter, but it’s still something I should get into the habit of doing even when it’s not needed. I have the bad habit of using “==” in general and “===” only when I know it’s needed because I learned “==” first and that’s how I started coding. In reality, however, the rule should be to use “===” in general and “==” only when you know you’ll need it. Thanks for the reminder. :slight_smile:


I’m sorry, my comment was for OP but I still have trouble with the whole reply thing :smiley:

I think as a general rule, you’d use “==” when the coercion effect is desired, but then again (this is my opinion there) I prefer to have control on the input, and manually change the value if desired. “==” does so much more than transforming a String to Number and vice versa.

We, who programmed our own stuff generally know what’s gonna be the input, be we need to take in account other people behaviors and have a secure way to filter their inputs.