# JavaScript Algorithms and Data Structures Projects - Cash Register

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 = []},
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
}else if(needed * y.Worth <= cidWorth) { //Mehr als genug im Kästchen
let worth = y.Worth * needed;
changeValue = Math.round((changeValue - worth)*100)/100;
ChangeCid(cid, y, worth);
} else { //nicht genug im Kästchen
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

Cant be more explanatory now but this would work:

``````  return {...change};
``````

Let me know if you found out why.

Tschuss mein Freund.

1 Like

thx a lot.
But why?! I dont get it

1 Like

You broke the object comparison script by adding methods to the returned object. An object with data alone is not the same thing as an object with data and methods.

1 Like

You compare using obj.hasOwnProperty ?

1 Like

Sounds correct! Try it!

1 Like

now i did but it seems to be the same also

1 Like

okay, so it’s just a testing thing and the code is technically ok?

This is what they use for testing:

assert.deepEqual()

Description

deepEqual( actual, expected, message = “” )

A recursive and strict comparison, considering all own and inherited properties.

More here. and the checking code from your exercise is here on FCC github

The code isn’t “technically” right (according to their testing set up.) But imho you solved the challenge just fine.

let change = new ChangeObj();

has also the methods that you defined, so the deep equality fails.

1 Like

I would suggest FCC them to just use hasOwnProperty for comparison, and not deep equality. I don’t know if that makes sense to you @JeremyLT @hbar1st. Maybe there is a reason why is has been done this way.

Well I have not looked into fcc code myself as of yet but if I had feedback it may be

1- to make clear to the learner that the expectation is to make a certain type of result (eg we expect the results to be an array or an array of objects or a single object etc)
2- once that is made clear, then the learner should adhere to it as it is part of “delivering on specifications”. That is, when a programmer is asked for something specific, they should work to provide the exact thing. (Not a lookalike)

2 Likes

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