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”)
{
console.log(“Oldest”);
}
if (whichChild = “Michelle”)
{
console.log(“Middle”);
else
{
console.log(“Not my kid!”)
}
}
function whichChild(name) {
if (name == "Dave") {
return "Oldest";
} else if (name == "Michelle") {
return "Middle";
} else {
return "Not my kid!";
}
}
console.log(whichChild("Michelle"));
Some things to note:
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.
“whichChild” is the name of the function. What you want to compare is the parameter value “name”.
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;
};
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.
I’m sorry, my comment was for OP but I still have trouble with the whole reply thing
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.