Hey Guys,
i think i get the correct results for all tests in the last challenge BUT only the first test is true → my result is an object.
I dont understand whats the problem in my output.
Please give my some support to get this done
Here is one of my outputs via console:
{ status: 'CLOSED',
change:
[ [ 'PENNY', 0.5 ],
[ 'NICKEL', 0 ],
[ 'DIME', 0 ],
[ 'QUARTER', 0 ],
[ 'ONE', 0 ],
[ 'FIVE', 0 ],
[ 'TEN', 0 ],
[ 'TWENTY', 0 ],
[ 'ONE HUNDRED', 0 ] ] }
and that’s how it should be:
{status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}
**Your code so far**
const Money = {
Penny: {Worth: 0.01, String: "PENNY"},
Nickel: {Worth: 0.05, String: "NICKEL"},
Dime: {Worth: 0.1, String: "DIME"},
Quarter: {Worth: 0.25, String: "QUARTER"},
Dollar: {Worth: 1, String: "ONE"},
FiveDollar: {Worth: 5, String: "FIVE"},
TenDollar: {Worth: 10, String: "TEN"},
TwentyDollar: {Worth: 20, String: "TWENTY"},
OneHundredDollar: {Worth: 100, String: "ONE HUNDRED"}
};
function ChangeObj() {
this.status = "",
this.change = [];
}
ChangeObj.prototype = {
SetStatusOpen: function() {
this.status = "OPEN";
//this.change.sort((a, b) => a - b);
this.change = this.change.filter(x => x[1] > 0 );
},
SetStatusClosed: function() {
this.status = "CLOSED";
for(let i = this.change.length - 2 ; i >= 0 ; i--){
this.change.push(this.change[i]);
this.change.splice(i,1);
}
},
SetStatusInsufficient: function() {
this.status = "INSUFFICIENT_FUNDS";
this.change = []},
AddChange: function(string, value) {
this.change.push([string, value]);
}
}
function checkCashRegister(price, cash, cid) {
let changeValue = Math.round((cash - price)*100)/100;
let count = 0;
let change = new ChangeObj();
let cidSum = 0;
cid.forEach((x) => {cidSum += x[1]});
cidSum = Math.round(cidSum * 100)/100 //Cidsumme neu berechnen und auf 2 Kommastallen runden, da irgendwie auf 8 Nachkommastellen etwas verloren geht
// Gehe jede Währungseinheit durch //komplett überarbeiten und bei den Loops entscheiden welcher Status angewendet wird. Wenn 0,01 und 5 da sind, kann ich nicht 1 wechseln
for(let x of Object.entries(Money).sort((a,b) => b[1].Worth - a[1].Worth)) {
let y = x[1];
let cidWorth = cid.filter(x => x[0] == y.String)[0][1];
let needed = Math.floor(changeValue / y.Worth);
if(cidWorth === 0) { //Kein Geld im Kästchen
change.AddChange(y.String, 0);
}else if(needed * y.Worth <= cidWorth) { //Mehr als genug im Kästchen
let worth = y.Worth * needed;
change.AddChange(y.String, worth);
changeValue = Math.round((changeValue - worth)*100)/100;
ChangeCid(cid, y, worth);
} else { //nicht genug im Kästchen
change.AddChange(y.String, cidWorth);
changeValue = Math.round((changeValue - cidWorth)*100)/100;
ChangeCid(cid, y, cidWorth);
}
}
if(changeValue > 0) {change.SetStatusInsufficient()}
else if(cid.every(x => x[1] <= 0 )) {change.SetStatusClosed()}
else {change.SetStatusOpen()};
return change;
}
function ChangeCid(cid,y,worth) {
cid.forEach(array => {
if(array[0] === y.String) {array[1] -= worth};
});
}
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0
Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register
Link to the challenge: