Year of the Dog explainer

Aim of the puzzle: Use the return keyword to return a value from a function.
Walk through of solution: The puzzle begins with two variables declared, dogName (a string) and dogAge (a number). Feel free to change the values of the two variables to a different string and number as you see fit.

The function lengthOfName() is also declared. This function takes a string, uses the .length string method to calculate the length of the string, and then returns that value using the return keyword.

The function ageInHumanYears() has also been declared. It takes an age (a number) and calculates age * 7, but is not yet using the return keyword. Because there is no return, the function will instead return undefined.

Try running the starter code when the puzzle loads. Because ageInHumanYears() does not have the return keyword, the following text will be printed to the console:

'The length of the name Pooky Mulder is 12 characters.'
'Pooky Mulder is undefined in human years.'

ageInHumanYears() is still performing the calculation age * 7, but it isn’t returning the answer, so undefined is being printed instead.

To complete the puzzle, replace age * 7 with return age * 7. Now when the program runs, the correct text will print to the console:

> 'The length of the name Pooky Mulder is 12 characters.'
> 'Pooky Mulder is 14 in human years.'

Because the return keyword has been added, the value of age * 7 can now be accessed outside the function.

Sample code solution:

let dogName = 'Pooky Mulder';
let dogAge = 2;

function lengthOfName(name) {
    return name.length;
}
function ageInHumanYears(age) {
    return age * 7;
}
console.log('The length of the name ' + dogName + ' is ' + lengthOfName(dogName) + ' characters.');
console.log(dogName + ' is ' + ageInHumanYears(dogAge) + ' in human years.');

Javascript Concepts: Return, Function Declarations, Variables with Let, String Methods

I cant make age * 7 to return age * 7. Someone explain me. I look to the hint but i cant nothing. This not replacing i try and i see only highlighted keyword return. in

2 Likes

To turn age * 7 into return age * 7, you’ll need to delete the existing line of code and rewrite it starting with return.

I’ll see if we can update the app to allow return to be added to the beginning of a line.

–Frankie

1 Like

Where can I practice coding more? It is very difficult for me to understand some levels, however I really enjoy coding. I really to learn code, don’t know where to start.

1 Like

There are some options in the app you might not have seen.

There is a Practice course that you can select from the main menu, on the left side. This is a course that grows as you solve more puzzles and helps you get more experience with code that you’ve already learned. This is a good place to go if you’ve taken a break and need a refresher.

There is also the Playground which is open ended. You can test out code and there isn’t any specific goal or feedback.

–Frankie

1 Like

Won’t let me type return age*7 even though that what’s it says in instructions

Hey there, tap/click the return key on the code keyboard to add a return statement, then add age * 7 using the other keys.

If you’re still having issues, try posting a screenshot and I can take a look.

1 Like

So I’m getting the right results but for some reason it will not accept it

1 Like

Hey there,

In your ageInHumanYears() function, change return dogAge * 7 to return age * 7.

That should do the trick.

Hope this helps!
Ben

1 Like

How does the function know that you mean “dogage” while you type “age”?

Same applys to “dogname” and “name”?

1 Like

Hey there, this is because age is the parameter of the ageInHumanYears() function and dogAge is the argument the function is called with.

For example:

function double(num) { // num is the parameter
    return num * 2;
}

double(3); // 3 is the argument

When ageInHumanYears() is called, whatever argument is in its parentheses gets stored in age. The function then returns age * 7.

In this case, we’re passing in a variable named dogAge as an argument. dogAge is storing the value 2, so this means the age parameter gets set to 2. The function then returns 2 * 7, which is 14.

Hope this helps!
Ben

2 Likes

When is the return command needed? I don’t get what purpose it serves…it doesn’t log anything to the screen or store a value in a variable

1 Like

Hey there, great question. The return command tells a function to stop running and return a value. We can then use this returned value in our code, for example:

let randomNumber = pickRandom(10)
// Grasshopper's pickRandom function returns a random number between 0 and 10
// which we can store in the randomNumber variable

Hope this helps! Let me know if you have any questions.
Ben

1 Like