# Money Changer explainer

The aim of this puzzle: Use the `inYen` function to convert US dollars into Japanese yen.

Walkthrough of the solution: The starter code has the `inYen()` function already completed for you. If you give it a number (of dollars) in its parentheses (like putting a number in `pickRandom()`) it will give back how many Japanese yen it’s worth.

The lines of code at the bottom print out the number that’s going to be converted, and then runs the `inYen()` function using that number.

The last line of the starter code prints out “100 US dollars is:”. To convert 100 dollars into yen, you use `inYen(100)`. That will calculate the correct number, but you won’t see the value on the screen unless you put it inside `console.log()`. This command should go at the very bottom of your code.

Sample code solution:
(Tap below to reveal)

``````function inYen(dollars) {
let amount = dollars * 113;
return amount;
}

console.log('20 US dollars is:');
console.log(inYen(20) + ' Japanese yen');
console.log('100 US dollars is:');
console.log(inYen(100) + ' Japanese yen');

``````

JavaScript Concepts: Arithmetic Operators (*), Binary Expression (+ concatenation), Calling Functions, Code Block (function), `console.log()`, Variable Scope (let)

7 Likes

Is it possible to see the starter code in every level? How is inyen connected to amount?

2 Likes

There is no hidden code in this puzzle like some of the earlier ones. When I say “starter code”, I mean the code that is visible on the screen when you first start the puzzle, or reset it.

When you call the `inYen()` function, it will create its own `amount` variable which gets a starting value equal to `dollars * 113`. `dollars` is called a parameter, it’s a placeholder variable that gets a value only when you call the `inYen()` function.

For example, `inYen(20)` means, “run the `inYen` function and use `20` as the input value”. That will store `20` inside of `dollars`. Then, `amount` gets created and has a value of `dollars * 113` which is equal to `20 * 113` for this specific time running the function. `20 * 113` is `2260`, so that is the final value for `amount`. Then the `amount` (`2260`) gets “returned” which means that’s the final output of the function call. We started out with the code `inYen(20)`, and in the end, we can simply substitute that with the number `2260`.

–Frankie

13 Likes

what does the ‘amount’ means or does here?

1 Like

`amount` is a variable used to store the result of `dollars * 113`.

The `amount` variable isn’t really necessary, as you could also write the function like this:

``````function inYen(dollars) {
return dollars * 113;
}
``````

Hope this helps!
Ben

5 Likes

What does the blank in
function inyen(dollars, ___)
Stand for

3 Likes

That’s nothing but a feature of the app. While coding you need not type such a blank. The blank just indicates that there is a place for input. The blank is to be ignored if you don’t have anything to put there…

3 Likes

@ParkleZ thanks

What does return mean? I don’t think this statement is explained in terms of creating a function.

5 Likes

Hey there, good question!

The `return` keyword tells a function to stop executing and return a value. This value can then be stored in a variable or used in another function.

In this puzzle, the `inYen()` function takes the parameter `dollars`, multiplies it by 113, and then returns the result.

When the following line of code runs:

``````console.log(inYen(20) + ' Japanese yen')
``````

The `console.log()` function sees the `inYen(20)` function call, and says, “Ok, I have to run this function first. Once it returns a value, I will then print that value.”

This means that

``````console.log(inYen(20) + ' Japanese yen')
``````

becomes

``````console.log(2260 + ' Japanese yen')
``````

which prints “2260 Japanese yen”.

Not all functions need to return a value. For example, the `drawBox()` function draws a box, but doesn’t return anything. `console.log()` prints text to the console, but doesn’t return anything either.

If a function isn’t told to return a value, it instead returns `undefined`.

You can see this behavior if you open your browser’s code console (on Chrome: Cmd `⌘` + Shift `⇧` + J or Ctrl `⌃` + Shift `⇧` + J. On Firefox: Cmd `⌘` + Option `⌥` + K or Ctrl `⌃` + Option `⌥` + K).

Type in `console.log('hello world')`, and you’ll see it print “hello world”, followed by `undefined`, like this:

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

5 Likes

the semicolon is added after function declrations is over i.e after the {}. is it right thing to follow

Non of the options given in little more change have baht, how do I go about it?

1 Like

I’m doing the code right, but it says that I need to Try updating the 0 in console.log(0 + ’ Japanese yen’); to use inYen(100).help?Here is a screenshot

You need the key function “inYen()” and inside the function you use the key “num” with the number 100 . Instead you replaced the zero with 100 using only the “num” key

I want to know how the “return” statement works, please can someone help me out…

1 Like

You can continue the course it will be explained, but I will explain it to you if you want to. The return statement like the name suggests returns something. After calling the function ‘‘inYen’’ in the console.log, it will be executed. After the execution, this function will output amount(amount will be a particular number) because you ask it to return amount, so it returns(output) it.
Is that clear?

1 Like

Hey there, good question.

Sometimes, you want to call a function, and have it return a value. You can then store this value in a variable, or maybe even use it in another function.

For example, let’s look at the following function:

``````function double(num) {
return num * 2;
}
console.log(double(5))
// double(5) executes and returns 10...
// then console.log(10) executes and prints 10
``````

Not all functions require return statements. For example, the `drawBox()` function we use in Fundamentals I takes a color name and then draws a box of that color. However, it doesn’t return a value.

Hope this helps!
Ben

3 Likes

Thanks a lot, I appreciate your effort in giving this explanation…

Yes, actually I’m beginning to understand it better. Thanks a lot…

1 Like

Please for a second time I’ll need your help, and it’s on the same issue. The return function is what I really want to get in clarity. Now, we say that each time the function name is called then it’s returned back to where it was first called. Please put me through in details on how this works from the start to finish, I’m really having a rough time understanding it, I’ll appreciate your assistance. Thanks…