Cash Without Demand 💻💵
2018-Oct-09, Tuesday 08:55![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Last night, after a period of struggle, I finally finished all of the JavaScript exercises on Free Code Camp post-revamp. I did a few before they changed the entire curriculum around, like the ROT13 cipher that I posted about here, but post-revamp there are five major projects to complete the certification. Here's the last one that took me the longest: a cash register.
I ran into two major problems while I was writing the code. The first was the While loop, where it kept quitting out without advancing the counter, and thus not checking every cash denomination in the register, and I couldn't figure out why. Eventually I added a second Else to advance the counter if either If statement was false, and then I remembered you can check for multiple conditions and only execute the If all of them are true. That took me like four hours to remember and when I added the &&, everything worked and I had my answer in the final form.
The second was with the last test case in the original problem. The answer was formatted strangely, with money returned from smallest denomination to largest rather than just the change given to the customer. This really confused me--did I need to add some kind of special condition to handle just this case? Why was it so different? What was I missing?
It turns out I didn't read the problem.
The problem specifically specifies that if the change is all of the remaining contents of the register, return the entire register pre-transaction, which will probably have most drawers empty. Once I realized that, and reformatted the early part of the problem to function on a copy of the cid variable rather than on cid itself, it all worked out. And just now writing this I realized I don't need cidRemaining, which is a relic from before I added the workingCID variable. And there's a console.log still in there I should get rid of.
There. Did that. 💮
I managed to solve this without looking up any other solutions or checking forums for other people asking questions. Neither of those are bad, and in fact they are essential to a programmer's work (see this classic video for an succinct explanation), but I'm still proud I came up with this solution all on my own. The one thing I did have to look up was how to use map() on multidimensional arrays, which led to the understanding that I can map() on each instance of another map(), and that was the final puzzle bit I needed to finish off the function.
Next up on the curriculum is the section on libraries (Bootstrap, jQuery, React, etc), but tonight I'm going to take a break. Maybe I won't even study Japanese!
Well, I won't get ahead of myself.
function checkCashRegister(price, cash, cid) {
let change = [["ONE HUNDRED",0], ["TWENTY",0], ["TEN",0], ["FIVE",0], ["ONE",0], ["QUARTER",0], ["DIME",0], ["NICKLE",0], ["PENNY",0]];
let difference = (cash - price) * 100;
const denoms = [10000, 2000, 1000, 500, 100, 25, 10, 5, 1]; //values in cents
let counter = 0;
let answer = {};
let workingCID = Array.from(cid); // since cid is needed for CLOSED, we need a new variable to work with
workingCID = workingCID.reverse().map(item => Math.round(item[1] * 100));
while (counter < 9) {
if (((difference - denoms[counter]) >= 0) && (workingCID[counter] > 0)) {
workingCID[counter] -= denoms[counter];
difference -= denoms[counter];
change[counter][1] += denoms[counter];
} else {
counter++;
}
}
if (difference > 0) {
return {status: "INSUFFICIENT_FUNDS", change: []};
} else {
change = change.filter(item => item[1] > 0);
let cidRemaining = workingCID.filter(item => item > 0);
if (cidRemaining.length === 0) {
answer.status = "CLOSED";
answer.change = cid;
console.log(answer);
return answer;
} else {
answer.status = "OPEN";
answer.change = change.map(item =>
item.map(value => value > 0 ? value / 100 : value));
return answer;
}
}
}
I ran into two major problems while I was writing the code. The first was the While loop, where it kept quitting out without advancing the counter, and thus not checking every cash denomination in the register, and I couldn't figure out why. Eventually I added a second Else to advance the counter if either If statement was false, and then I remembered you can check for multiple conditions and only execute the If all of them are true. That took me like four hours to remember and when I added the &&, everything worked and I had my answer in the final form.
The second was with the last test case in the original problem. The answer was formatted strangely, with money returned from smallest denomination to largest rather than just the change given to the customer. This really confused me--did I need to add some kind of special condition to handle just this case? Why was it so different? What was I missing?
It turns out I didn't read the problem.

There. Did that. 💮
I managed to solve this without looking up any other solutions or checking forums for other people asking questions. Neither of those are bad, and in fact they are essential to a programmer's work (see this classic video for an succinct explanation), but I'm still proud I came up with this solution all on my own. The one thing I did have to look up was how to use map() on multidimensional arrays, which led to the understanding that I can map() on each instance of another map(), and that was the final puzzle bit I needed to finish off the function.
Next up on the curriculum is the section on libraries (Bootstrap, jQuery, React, etc), but tonight I'm going to take a break. Maybe I won't even study Japanese!
Well, I won't get ahead of myself.
