A Little More Change explainer

The aim of this puzzle: Complete the inBaht function definition which will convert US dollars in to Thai baht.

Walkthrough of the solution: When you run the starter code, you’ll see that the amount of US dollars stored in the wallet (50). Below that it prints how much 50 dollars is worth in yen (5650). The inYen(wallet) and inBaht(wallet) function calls are the same, but the inBaht(wallet) call doesn’t produce any output. Let’s look at what the function is doing.

The inBaht function has 1 parameter, dollars, just like the inYen function. But it doesn’t do anything with it. We need to add code inside of the curly brackets {} of the function definition so it knows how to convert the money and output that number.

The first step is to do the conversion. Each dollar is worth 33 Baht. That means if we do dollars * 33 we get the amount of Baht. We should save that number in a new variable. We can use let baht to store that value.

Next in the function, we want to output our value. We use return to send the output back to where the inBaht() function was called. You can first tap the return button and then add baht to that. When you use return baht, the value of baht will take the place of inBaht(wallet) at the bottom of all your code.

Sample code solution:
(Tap below to reveal)

let wallet = 50;

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

function inBaht(dollars) {
  let baht = dollars * 33;
  return baht;
}

console.log(wallet + ' US dollars is:');
console.log(inYen(wallet) + ' Japanese yen');
console.log(inBaht(wallet) + ' Thai baht');

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

Verry difficult to understand how it exactly works. I dont understand inbath and dollar in function, how it is connected to the rest.

The function keyword creates a new function, similar to how var creates a variable. A function declaration has 3 parts: name, parameters, body.

inBaht is the name of the function. dollars is a parameter, which is an input variable. Later on when we call the function using inBaht(wallet), or inBaht(100), or any other number, the value in the parentheses is temporarily stored inside of the dollars variable. Between the curly brackets is the body of the function. It has 2 lines: calculate the new amount, and then return that amount. return is the keyword that tells the function what the output should be. Again, a parameter stores the input, and then return sends an output.

Now that the function is all filled out (the function has been defined, just like how a word has a definition), we can call it. Calling a function means telling it to run. drawBox(green) is calling the drawBox function. print('hi') is a call to the print function. inBaht(wallet) calls the inBaht function.

Here’s a smaller example. We’ll create a function that takes a number and then doubles it.

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

Let’s use this function.

let number = 3;
let biggerNumber = double(number);
console.log(biggerNumber);

This code would output the number 6.

–Frankie

4 Likes

I wanted to insert the let variable amount but I got “tap ___ to edit it”. After tapping it and trying to insert it I got the message again.

4 Likes

I had the same problem! The website would not let me edit that line of code.

4 Likes

Hey there, for naming things like variables and function parameters, tap twice on the underline to bring up the keyboard. Then enter the name manually.

You can have 2 variables with the same name (such as amount) if they live in completely different functions, and thus can’t see each other. However, you’ll still have to enter the name manually.

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

Thanks Ben!
Yes, this works!

Albert

1 Like

FYI It appears this puzzle is not recognizing the “amount” button. The desktop/web version wasn’t letting me add amount by clicking on the “amount” button, so I tried on the mobile app (iOS 12.4.2), which also didn’t let me use the “amount” button, but instead prompted me to type. I was then able to type in the word “amount” and complete the puzzle. I did the same on desktop: instead of selecting amount, I selected number, then typed in the word amount (in addition to following the rest of the instructions) and was able to get it to work.

So for anyone having trouble inserting amount, try inserting a number instead and type the word amount to bypass the error message telling you to tap to edit.

2 Likes

Hey there, please see this post here for an explanation of how adding parameters works in Grasshopper.

Thanks!
Ben

^ this was my AHA! moment :open_mouth: :bulb: with how functions work by replacing variables/ parameters. Thanks for the in-depth explanation Frankie!

Thanks. Still does not work for me. So I am skipping this lesson.
On another subject, how does the function know that the wallet value is passed to the dollars value?
Thanks.
let wallet = 50;
function inYen(dollars, ___) {
let amount = dollars * 113;
return amount;

Hi Ben,

I attempted this workaround, and it didn’t work. Double-clicking on the underline simply highlighted the characters. I am unable to move past this exercise b/c of this problem. Is there a way to resolve this? Thank you.

Jeff

Edit: I’ve entering all the available commands, and they all return the same error. I also tried skipping this lesson, but that doesn’t seem to be a possibility, either, so for the time being, I’m stuck.

This is exactly how I want to write it, but the system doesn’t accept it. There must be an error somewhere

Thanks for the hint, it was driving me nuts

Try it the Echo’ s way, it just worked for me :grinning:

Unbelievable… I still couldn’t get the function right… It won’t accept the “amount” or … I can’t win. BUT, I am not giving up!

1 Like

im stuck can i have help

Where are you stuck? What’s your problem?

Hey there, I’m happy to help, but you’ll need to be more specific. Let the forum know where you’re stuck or having trouble, and we’ll be happy to help you. You can even try posting a screenshot to get help with your code!