# JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
I need some help with making my logic work.
Basically
I have the dictionary I made. “currencyUnits”
so I want to get the lowest amount of change possible.
so I know for example i need the quarter.
0.5 / 0.2 gives me 2 times
so I want to take 0.25 twice out of the quarter and push it into my new array.
I am not sure how to actually write it in code.

Hope you understood what I was trying to explain.
thanks

``````function checkCashRegister(price, cash, cid) {
let totalCashInDrawer = 0;
let change = cash - price;
let changeArr = [];
const currencyUnits = {
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.1,
"QUARTER": 0.25,
"ONE": 1,
"FIVE": 5,
"TEN": 10,
"TWENTY": 20,
"ONE HUNDRED": 100
};

for (let i = 0; i < cid.length; i++) {
totalCashInDrawer += cid[i][1];
}
if (change > totalCashInDrawer) {
return {status: "INSUFFICIENT_FUNDS", change: [] };
}
else if (change === totalCashInDrawer) {
return { status: "CLOSED", change: cid }
}

else {
cid.reverse();
for (let i = 0; i<cid.length; i++) {
const unitsValue = currencyUnits[cid[i][0]]
const actualValue = cid[i][1]
let numberOfTimes = change / unitsValue
while (numberOfTimes > 1) {
changeArr.push(cid[i][0], change);
numberOfTimes--;
}

}

}
}

checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
/*

*/
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36`

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Well, sometimes its best to write out what you’re wanting to do in steps. I find scratching ideas/methods out on paper really helps me understand the goal and how to get there. I mean, you are basically writing the answer in your question… you need to remove 2 times 0.25 from your quarters in `cid` and then put 2 times 0.25 into your `change` array.

You’re code supplied isn’t complete so can’t really point out any particular mistakes or suggestions. Remember in the push, you’re wanting to push an array of two values, not just two values. Also, do you really want to push all your change, or just the amount of quarters you took out?

One other thing to note, your `numberOfTimes` seems to be how many quarters are needed to meet the change… but you also need to take into account how many quarters you have in cid… if you need 0.75 in change, but only have 0.50 in your drawer, you’d want to take out 2 quarters, not 3.

Not being sure how to solve a problem is I believe the point of the challenge, so I don’t want to give too much away. Keep at it, think about the problem, research different ways to write the code, and you’ll come up with something.

Second is there a command to take a specific amount out of an array data , if i have said it correctly?
Like out of 4.25 to pop 0.25.

Okay.
I am not home yet so I cannot actually code it.

If i take the 25 out can i split it back to 0.25?
And what happens when I dont have 25s anymore and the number becomes whole as in 4?

Lol, subtraction?

``````let value = ["QUARTERS", 4.25];  //This is an example array
value[1] -= .25;
console.log(value);     //This should not print out ["QUARTERS",4.0]
``````

Sorry, if you’re not familiar with the `-=` notation it is a shortcut for writing out subtraction from itself. EX: `x = x - 5;` is the same as writing `x -= 5;` .

1 Like

Right, thanks. I ll see what i can come up with when I code it

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.