So, today I want to look at the challenge that follows Symmetric Difference on FreeCodeCamp, named “Exact Change”.

Here’s the code we’re given to start with:

Let’s get the low hanging fruit first. I know I’m going to need a variable to hold what the total value of the cash in the drawer is. How to get this value I’ll look at in a little bit; for now, I’ll just give it a value of $0 and call it “totalCashID”.

I also know that I need to know how much change is needed and I’ll call that totChange. That’s easy to find; it’s just the cash the customer gives me minus the price of the item, or cash – price.

I know that I have some conditional tests, so I’ll set up an if…then…else statement. First, if the amount of change that is in the drawer (totCashID) is less than the change I need to give, then I return “Insufficient Funds”, else if if the amount of change that is in the drawer (totCashID) is equal to the change I need to give, I return “Closed”, else I process the transaction and return the person’s change, an object sorted according to the values of the change. I’ll figure out how to calculate change in a little bit.

So what I have so far is:

cid[0][1]

We can do this for each of the coins and bills in the drawer, assigning them to a variable. Then we add up the variables and get the totalChangeID value! That even passes four of the tests! Halfway there already! Here’s what the code looks like now:

So, now comes what I consider to be the hardest part: figuring out the change to return to the customer and breaking it down by bills and coins. It doesn’t seem that difficult; I know that I’m going to use the modulus operator, for example.

So let’s put it into operation and write some code. One more thing that I’ll point out is that if the amount is less than one you’ll be dividing by a decimal remainder and get ridiculously high amounts so make sure you have an if then statement for whether the change is more than one or less than one.

Here we go:

There’s a lot of repetition there and if this was the real world I would create a separate function for it.

Now we’ve got the amounts per coin, and we need to assemble it into an array with our old friend push. Note that trial and error showed me that FreeCodeCamp doesn’t want the number of coins returned; they want the monetary value. So when I push it to the array below I multiply to get the value.

However, this still isn’t right. FreeCodeCamp wants to only return the values of coins if they are greater than zero. I’ll have to re-write the statement:

Well, I’m closer. This would work if the register had unlimited change. However. in one of the tests I’m getting wrong, the register returns $80.00 in twenties, but there are only $60.00 in twenties in the drawer. Time to refactor, again.

UPDATE:

Well, I certainly did that the long way around. There were several things I had wrong in my code, and it took some bashing around to straighten it out. Here it is, section by section:

The opening, of course, and me declaring my multitude of variables that represent what we’re going to give back to the customer. Then, we have the actual change in the drawer – I explained this above. Next, we have the if…else if…else statement that I mentioned above. The contents of the else portion have changed, so I’ll look at those below.

Here’s the portion in between the brackets following else:

Now, as I said, I did this the hard way, so let me explain. There are two parts to the if statement – one if the change required is a dollar or more, and one if the change is less than one dollar. If this was a professional job, I would make the change a separate function rather than repeat it. I also had it calculate the number of coins rather than the value of the change; this is why the code is so tortured with conversions. I would change that too if I was doing this professionally but since I am a learner I want you to see some of the ways I worked to solve it originally. This also shows there’s more than one way to get to the right answer!

Next, I had to find a way to solve what would happen if I couldn’t give the customer any change despite the fact that I had enough total in the drawer, I just couldn’t divide what I had – so, for example, they should get 13 cents but all I have is one dollar and one nickel. I have $1.05, but I can’t divide it to make 13 cents. The test above for insufficient funds doesn’t catch this. But, if the amount of change I return is greater than the change required, I know I can’t return it. Thus:

See the toFixed function being used there? It stymied me for awhile because you need 2 decimal points, which is why I used it, but you get a string back, which is no help at all! After some web searching, I found that putting a plus sign (+) in front fixes that issue. So, finally, I need to return the array. The way I originally did it had it start with the pennies and progress to the hundreds; as it turns out, FreeCodeCamp doesn’t accept that answer and I had to re-arrange it so that it started with hundreds and descended to pennies:

And there you have it! The Exact Change challenge solved. Note that FreeCodeCamp’s answer uses an object and loops through it, so obviously I could have shortened this quite a bit; as I said though, now you can see that I’m no expert and this is how someone of intermediate knowledge of JavaScript might solve it!