JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
I apologize in advance for having such an awful and complicated code but I just didn’t have a better idea. Im interested in why does my code fail the test-case with parameters for price=3.26 and cash=100?

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

``````let indeks=0;

function checkCashRegister(price, cash, cid) {

let valute=[0.01,0.05,0.1,0.25,1,5,10,20,100];
let pomocna=[];
let kusur=0;

let changeObj={
status:"",
change:[]
};

for(let i=0;i<valute.length;i++){
if(valute[i]<=(cash-price)){
indeks++;
}
}
indeks=indeks-1;
console.log(indeks);

for(let i=indeks;i>=0;i--){
pomocna.push(cid[i]);
}
console.log(pomocna)

let total=0;
for(let i=0;i<pomocna.length;i++){
total+=pomocna[i][1];
}
if(total<(cash-price)){
changeObj.status="INSUFFICIENT_FUNDS";
changeObj.change=[];
return changeObj;
}

for(let i=0;i<pomocna.length;i++){

if(pomocna[i][0]==="ONE HUNDRED"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=100;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=100;
num-=100;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=100;
num-=100;
console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}

}
//////////////////////////////////
if(pomocna[i][0]==="TWENTY"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=20;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=20;
num-=20;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=20;
num-=20;
//console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
//console.log(kusur);
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}

}

/////////////////////////////////////
if(pomocna[i][0]==="TEN"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=10;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=10;
num-=10;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
console.log(kusur);
let num=kusur+pomocna[i][1];
console.log(num);
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=10;
num-=10;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
console.log(kusur);
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
}

////////////////////////////
if(pomocna[i][0]==="FIVE"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=5;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=5;
num-=5;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=5;
num-=5;
//console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
//console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
}
/////////////////////////////////////
if(pomocna[i][0]==="ONE"){
console.log(pomocna[i][1]);
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=1;
}

if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=1;
num-=1;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}

if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=1;
num-=1;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
}
/////////////////////////////////////
if(pomocna[i][0]==="QUARTER"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=0.25;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.25;
num-=0.25;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
console.log(pomocna[i][1]);
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.25;
num-=0.25;
//console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
//console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
}
////////////////////////////////////

if(pomocna[i][0]==="DIME"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=0.1;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.1;
num-=0.1;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.1;
num-=0.1;
//console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
pomocna[i][1]=(pomocna[i][1]).toFixed(1);
//console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}

}

/////////////////////////////////////
if(pomocna[i][0]==="NICKEL"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=0.05;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.05;
num-=0.05;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.05;
num-=0.05;
//console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
pomocna[i][1]=(pomocna[i][1]).toFixed(2);
//console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}

}

}
//////////////////////////////////
if(pomocna[i][0]==="PENNY"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=0.01;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.01;
num-=0.01;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.01;
num-=0.01;
//console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
pomocna[i][1]=(pomocna[i][1]).toFixed(2);
console.log(pomocna[i][1]);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
}

////////////////////////////
//console.log(pomocna);
//changeObj.change.push(cid[i]);

}
console.log(pomocna);
return changeObj;
}

console.log(checkCashRegister(3.26, 100, [["PENNY", 1.01],
["NICKEL", 2.05],
["DIME", 3.1],
["QUARTER", 4.25],
["ONE", 13],
["FIVE", 55],
["TEN", 20],
["TWENTY", 60],
["ONE HUNDRED", 100]]));
``````

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Look closely at what is being returned in your console log:

``````{ status: 'OPEN',
change:
[ [ 'TWENTY', 60 ],
[ 'TEN', 20 ],
[ 'FIVE', 15 ],
[ 'ONE', 1 ],
[ 'QUARTER', 0.5 ],
[ 'DIME', '0.2' ],
[ 'PENNY', '0.04' ] ] }
``````

Are all of those amounts numbers?

1 Like

Thank you for pointing that out.

I have fixed that problem but the test-case still fails.

``````let indeks=0;

function checkCashRegister(price, cash, cid) {

let valute=[0.01,0.05,0.1,0.25,1,5,10,20,100];
let pomocna=[];
let kusur=0;

let changeObj={
status:"",
change:[]
};

for(let i=0;i<valute.length;i++){
if(valute[i]<=(cash-price)){
indeks++;
}
}
indeks=indeks-1;
console.log(indeks);

for(let i=indeks;i>=0;i--){
pomocna.push(cid[i]);
}
console.log(pomocna)

let total=0;
for(let i=0;i<pomocna.length;i++){
total+=pomocna[i][1];
}
if(total<(cash-price)){
changeObj.status="INSUFFICIENT_FUNDS";
changeObj.change=[];
return changeObj;
}

for(let i=0;i<pomocna.length;i++){

if(pomocna[i][0]==="ONE HUNDRED"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=100;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=100;
num-=100;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=100;
num-=100;
console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}

}
//////////////////////////////////
if(pomocna[i][0]==="TWENTY"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=20;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=20;
num-=20;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=20;
num-=20;
//console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
//console.log(kusur);
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}

}

/////////////////////////////////////
if(pomocna[i][0]==="TEN"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=10;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=10;
num-=10;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
console.log(kusur);
let num=kusur+pomocna[i][1];
console.log(num);
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=10;
num-=10;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
console.log(kusur);
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
}

////////////////////////////
if(pomocna[i][0]==="FIVE"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=5;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=5;
num-=5;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=5;
num-=5;
//console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
//console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
}
/////////////////////////////////////
if(pomocna[i][0]==="ONE"){
console.log(pomocna[i][1]);
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=1;
}

if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=1;
num-=1;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}

if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=1;
num-=1;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
}
/////////////////////////////////////
if(pomocna[i][0]==="QUARTER"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=0.25;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.25;
num-=0.25;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
console.log(pomocna[i][1]);
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.25;
num-=0.25;
//console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
//console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
}
////////////////////////////////////

if(pomocna[i][0]==="DIME"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=0.1;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.1;
num-=0.1;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.1;
num-=0.1;
//console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
pomocna[i][1]=Number((pomocna[i][1]).toFixed(1));
//console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}

}

/////////////////////////////////////
if(pomocna[i][0]==="NICKEL"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=0.05;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.05;
num-=0.05;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.05;
num-=0.05;
//console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
pomocna[i][1]=Number((pomocna[i][1]).toFixed(2));
//console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}

}

}
//////////////////////////////////
if(pomocna[i][0]==="PENNY"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=0.01;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.01;
num-=0.01;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.01;
num-=0.01;
//console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
pomocna[i][1]=Number((pomocna[i][1]).toFixed(2));
console.log(pomocna[i][1]);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
}

////////////////////////////
//console.log(pomocna);
//changeObj.change.push(cid[i]);

}
console.log(pomocna);
return changeObj;
}

console.log(checkCashRegister(3.26, 100, [["PENNY", 1.01],
["NICKEL", 2.05],
["DIME", 3.1],
["QUARTER", 4.25],
["ONE", 13],
["FIVE", 55],
["TEN", 20],
["TWENTY", 60],
["ONE HUNDRED", 100]]));
``````

Here is my completely finished code.

``````let indeks=0;

function checkCashRegister(price, cash, cid) {

let valute=[0.01,0.05,0.1,0.25,1,5,10,20,100];
let pomocna=[];
let kusur=0;

let changeObj={
status:"",
change:[]
};

for(let i=0;i<valute.length;i++){
if(valute[i]<=(cash-price)){
indeks++;
}
}
indeks=indeks-1;
console.log(indeks);

for(let i=indeks;i>=0;i--){
pomocna.push(cid[i]);
}
console.log(pomocna);

let total=0;
for(let i=0;i<pomocna.length;i++){
total+=pomocna[i][1];
}
console.log(total);
if(cash<price){
console.log("Daj mi još nigg!");
return changeObj;
}
if(total<(cash-price)){
changeObj.status="INSUFFICIENT_FUNDS";
changeObj.change=[];
return changeObj;
}

if(total===(cash-price)){
for(let i=0;i<cid.length;i++){
changeObj.status="CLOSED";
changeObj.change.push(cid[i]);
}
return changeObj;
}

for(let i=0;i<pomocna.length;i++){

if(pomocna[i][0]==="ONE HUNDRED"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=100;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=100;
num-=100;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=100;
num-=100;
console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}

}
//////////////////////////////////
if(pomocna[i][0]==="TWENTY"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=20;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=20;
num-=20;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=20;
num-=20;
//console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
//console.log(kusur);
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}

}

/////////////////////////////////////
if(pomocna[i][0]==="TEN"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=10;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=10;
num-=10;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
console.log(kusur);
let num=kusur+pomocna[i][1];
console.log(num);
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=10;
num-=10;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
console.log(kusur);
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
}

////////////////////////////
if(pomocna[i][0]==="FIVE"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=5;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=5;
num-=5;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=5;
num-=5;
//console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
//console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
}
/////////////////////////////////////
if(pomocna[i][0]==="ONE"){
console.log(pomocna[i][1]);
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=1;
}

if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=1;
num-=1;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}

if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=1;
num-=1;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
}
/////////////////////////////////////
if(pomocna[i][0]==="QUARTER"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=0.25;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.25;
num-=0.25;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
console.log(pomocna[i][1]);
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.25;
num-=0.25;
//console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
//console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
}
////////////////////////////////////

if(pomocna[i][0]==="DIME"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=0.1;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.1;
num-=0.1;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.1;
num-=0.1;
//console.log(num);
//console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
pomocna[i][1]=Number((pomocna[i][1]).toFixed(1));
//console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}

}

/////////////////////////////////////
if(pomocna[i][0]==="NICKEL"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=0.05;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
console.log(kusur);
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.05;
num-=0.05;
console.log(num);
console.log(pomocna[i][1]);
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
console.log(kusur);
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.05;
num-=0.05;
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
pomocna[i][1]=Number((pomocna[i][1]).toFixed(2));
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}

}

}
//////////////////////////////////
if(pomocna[i][0]==="PENNY"){
while(pomocna[i][1]>(cash-price)){
pomocna[i][1]-=0.01;
}
if(pomocna[i][1]===(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.01;
num-=0.01;
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}

if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
if(pomocna[i][1]<(cash-price)){
changeObj.status="OPEN";
if(kusur<(cash-price)){
let num=kusur+pomocna[i][1];
if(num>(cash-price)){
while(num>(cash-price)){
pomocna[i][1]-=0.01;
num-=0.01;
}
}
}
kusur+=pomocna[i][1];
if(kusur===(cash-price)){
changeObj.change.push(pomocna[i]);
break;
}
pomocna[i][1]=Number((pomocna[i][1]).toFixed(2));
if(pomocna[i][1]>0){
changeObj.change.push(pomocna[i]);
}
}
}

////////////////////////////

}
console.log(pomocna);
return changeObj;
}

console.log(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]]));
``````

I forgot to point out in my last post that altough it is completed and has correct outputs it still fails the third and the fifth test case but I do not know why. If anybody can help out to clarify why is it like that I would be very grateful.

Get rid of the console.log after the function and I think you will be in or a pleasant surprise.

1 Like

Why is this floating around in the global space? Using global variables in your function is a big red flag!

2 Likes

Thank you very much

1 Like

Got rid of that thanks for the heads up. It works now

1 Like