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)

2 Likes

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

1 Like

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

7 Likes

what does the ‘amount’ means or does here?

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

2 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 :grin:

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

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

2 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?

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