# Penny Pincher explainer

Aim of the puzzle: Complete the `inCents()` function to convert dollars into pennies.
Walk through of solution: The starter code begins by declaring the variable `wallet` and giving it the value `20`. Below that the function `inCents()` is declared, but not finished. It takes the parameter `dollars` and assigns this to `wallet`.

Finish the `inCents()` function by tapping on `dollars` inside the body `{ }` of the function to highlight it and bring up the keyboard. Then use the keys `*` and `num` to multiply `dollars * 100`. When this function is finished, run the code to complete the puzzle.

When the program runs, the code will print the first `console.log()` statement, which prints out the value of `wallet`. The `inCents()` function is then called, and given `wallet` as an argument. The function will multiply this number by 100 and assign it back to `wallet`. The second `console.log()` statement will then print out the value of `wallet`, which is now 2000.
Sample code solution:

``````let wallet = 20;

function inCents(dollars) {
wallet = dollars * 100;
}

console.log('You have ' + wallet + ' dollars.');
inCents(wallet);
console.log('You have ' + wallet + ' cents.');
``````

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

I dont understand why we have defined âwalletâ but we didnt need to assign âdollarsâ. How does the computer know that âdollarsâ refers to the value of âwalletâ?

The `dollars` variable is called a Parameter. When a function is declared, like `function inCents(...)`, the parentheses may contain a parameter. The parameter will get assigned a value once the function is called (âcalling a functionâ just means ârunning a functionâ).

In this puzzle, the `inCents(...)` function is called with `wallet` as the argument. This means the `wallet` variable, which is storing the value `20`, gets plugged into the `inCents()` function as the new value for the `dollars` parameter.

Parameters: placeholder variables that are used inside a function declaration
Arguments: values assigned to the parameter when calling the function

``````function add(x, y) {
console.log(x + y);
}

``````

In this example, `x` and `y` are parameters and the numbers `10` and `20` are arguments. When you call a function, you can imagine a step where each of the arguments gets stores in the parameters. Itâs important that you have the arguments in the right order.

``````         add(10, 20);
â   â
function add( x,  y) {
console.log(x + y);
}
``````

âFrankie

1 Like

I donât understand the use of Let here. I understand what it does, but in the glossary they explain that a Let can only be used inside the code block { } where itâs called. However, here itâs used outside of a block, so how does it differ from Var in this case?

In this case, `var` and `let` would behave the same way. You can imagine the entire code as being wrapped inside of a big code block. Since everything is inside that block, the `wallet` can be used anywhere in the code.

``````{
let wallet = 20;

function inCents(dollars) {
wallet = dollars * 100;
}

console.log('You have ' + wallet + ' dollars.');
inCents(wallet);
console.log('You have ' + wallet + ' cents.');
}
``````

Another special thing about `var` is that when your code runs, it will actually take a sneak peak ahead and check if there are any `var`s created, and it will let you use it before its created (kind of a strange thing).

âFrankie

1 Like

I dont understand; why am I getting the correct result from my code yet itâs not letting me go to the next puzzle. What have I done wrong?

Hey there, take the `* 100` out of the function call, and move it to the function declaration, like this:

``````function inCents(dollars) {
wallet = dollars * 100;
}

inCents(wallet);
``````
1 Like