Year of the Pig explainer

The aim of this puzzle: Use the return keyword to return a value from a function.

Walkthrough of the solution: The return keyword tells a function to return a value to the caller.

Not all functions require return statements. For example, console.log() will output text to the console, but is not used to return a value.

In this puzzle, the lengthOfName function takes a parameter called name, and then calculates name.length. However, it is missing the return keyword, so this value is not able to leave the function, and is lost as soon as it is calculated.

This means that when the function is called inside the console.log() statement, it will return undefined.

To complete the puzzle, use the return keyword to return name.length. Now when the function is called, this value will be passed to the console.log().

Sample code solution:
(Tap below to reveal)

let pigName = 'Wilbur';
let pigAge = 1;

function lengthOfName(name) {
  return name.length;
}
function ageInHumanYears(age) {
  return 14 + 4 * age;
}
console.log('The length of the name ' + pigName + ' is ' + lengthOfName(pigName) + ' characters.');
console.log(pigName + ' is ' + ageInHumanYears(pigAge) + ' in human years.');

JavaScript Concepts: Return Statements, Console, Strings, Function Declarations, Calling Functions, Variable Declarations (let)

2 Likes

In this puzzle how lengthofname have parameter as name. I mean to say that how we can know that name is pigname where it is declared

1 Like

Great question!

A parameter is like a special variable that lives inside the function. When the function is called, whatever is passed in as an argument will behave like the parameter.

For example:

function double(num) {
    return num * 2;
}

double(5)

In the above example, num is the parameter, while 5 is the argument.

In this puzzle, name is the parameter, and pigName is the argument.

Hope that makes sense! Let me know if you have any questions.
Ben

1 Like

If there are more than one function then how parmeter verify the arguments
Similarly if there are more than one arguments then how parmeter verify it

As in above puzzel there are two argument that is pigname and pigage then how it verify the pig name with name parameter and pigage with age parameter
How pigname and name parameter are connected
And how pig age is connected with age

2 Likes

If you add a parameter to a function, like this:

function double(number) {
  return number * 2;
}

Any argument placed in the parentheses when the function is called, like double(5), will be treated like number.

If extra arguments are placed in a function, they are ignored.

For example, if you tried to call the function like this: double(5, 4, 3), then the 4 and the 3 are ignored, because the function hasn’t been told what to do with them.

Great questions!
Ben

1 Like

I’m not able to type in return age*7 I’m on a pixel

Just trying out: In the function lengthofname, the code inside the brackets(return name.length)is what will be run…

Therefore when you use that function taking the argument pigname, the code that runs inside the function lengthofname is used to process the scope variable pigname (whose characters was Wilbur )

I don’t understand what the return keyword does. Can someone eexplain to me?

Hi which country is yours