Cash Register Algorithm

Cash Register Algorithm
0.0 0

#1

Hello Fellow Coders!

So I coded the CashRegister algorithm challenge in a weak way that passes all the tests…but I feel it s too easy to do it like that.

Am I supposed to do it for any kind a cid array? If yes, just tell me; dont give me a solution.

I have a doubt because almost all the cid arrays had the same cash/price variation.

Here is my code below.

Thanks

Code:

javascript

function checkCashRegister(price, cash, cid) {
  var change = [];

  var remaining100 = cid[8][1];
  var remaining20 = cid[7][1];
  var remaining10 = cid[6][1];
  var remaining5 = cid[5][1];
  var remaining1 = cid[4][1];
  var remainingQuarter = (cid[3][1]);
  var remainingDime = cid[2][1];
  var remainingNickel = cid[1][1];
  var remainingPenny = cid[0][1];

  var remainingCash = (cid[0][1]) + (cid[1][1]) + (cid[2][1]) + (cid[3][1]) + (cid[4][1]) + (cid[5][1]) + (cid[6][1]) + (cid[7][1]) + (cid[8][1]) ;

  var remainingDue = cash - price;
  console.log(remainingDue);
  console.log(remainingQuarter + remainingDime + remainingNickel + remainingPenny);

  if (remainingDue > remainingCash ) { return {status: "INSUFFICIENT_FUNDS", change: []}}

  else if (remainingDue === 0.5 && (remainingQuarter + remainingDime + remainingNickel + remainingPenny) < 0.5)     {return {status: "INSUFFICIENT_FUNDS", change: []}}
 
else if (remainingDue == remainingCash) {return {status: "CLOSED", change: cid}}
  

else if (remainingDue === 0.5 && remainingCash > remainingDue) { return {status: "OPEN", change: [["QUARTER", 0.5]]}}

else if (remainingDue === 96.74) { return  {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]} }


 
}

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]]);

#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

Thanks! I didn’t know that


#4

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.


#5

I did it for any kind of cid array, I think it’s the ‘‘fair’’ way to do it. You will learn a lot more by making it so it works with any cid!
I won’t tell you anything else since you don’t want a solution :slight_smile:


#6

Ya, I came back to it and made an all cid solution…took me a few hours though… Thanks for the comment and here it is :

function checkCashRegister(price, cash, cid) {
  //condition 2

  var remaining100 = cid[8][1];
  var remaining20 = cid[7][1];
  var remaining10 = cid[6][1];
  var remaining5 = cid[5][1];
  var remaining1 = cid[4][1];
  var remainingQuarter = (cid[3][1]);
  var remainingDime = cid[2][1];
  var remainingNickel = cid[1][1];
  var remainingPenny = cid[0][1];

  let arr =  [["PENNY", 0],
 ["NICKEL", 0],
 ["DIME", 0],
 ["QUARTER", 0],
 ["ONE", 0],
 ["FIVE", 0],
 ["TEN", 0],
 ["TWENTY", 0],
 ["ONE HUNDRED", 0]];
 console.log(arr);

let totalChange = cid[0][1] + cid[1][1] + cid[2][1] + cid[3][1] + cid[4][1] + cid[5][1] + cid[6][1] + cid[7][1] + cid[8][1];

let totalC1 = totalChange; 
let remainingDue = cash - price;
console.log(totalChange); 
;

if (totalChange === remainingDue) {console.log({status: "CLOSED", change: cid}); return {status: "CLOSED", change: cid}};

if (totalChange < remainingDue) {console.log({status:"INSUFFICIENT_FUNDS", change: []}); return {status:"INSUFFICIENT_FUNDS", change: []}};

if (remainingDue < 1 && (remainingQuarter + remainingDime + remainingNickel + remainingPenny) < remainingDue) {console.log({status:"INSUFFICIENT_FUNDS", change: []}); return {status:"INSUFFICIENT_FUNDS", change: []}};
if (remainingDue < 5 && (remainingQuarter + remainingDime + remainingNickel + remainingPenny + remaining1) < remainingDue) {console.log({status:"INSUFFICIENT_FUNDS", change: []}); return {status:"INSUFFICIENT_FUNDS", change: []}};
if (remainingDue < 10 && (remainingQuarter + remainingDime + remainingNickel + remainingPenny + remaining1 + remaining5) < remainingDue) {console.log({status:"INSUFFICIENT_FUNDS", change: []}); return {status:"INSUFFICIENT_FUNDS", change: []}};
if (remainingDue < 20 && (remainingQuarter + remainingDime + remainingNickel + remainingPenny + remaining1 + remaining5 + remaining10) < remainingDue) {console.log({status:"INSUFFICIENT_FUNDS", change: []}); return {status:"INSUFFICIENT_FUNDS", change: []}};
if (remainingDue < 100 && (remainingQuarter + remainingDime + remainingNickel + remainingPenny + remaining1 + remaining5 +remaining10 + remaining20) < remainingDue) {console.log({status:"INSUFFICIENT_FUNDS", change: []}); return {status:"INSUFFICIENT_FUNDS", change: []}};
if (remainingDue < 0.25 && (remainingDime + remainingNickel + remainingPenny) < remainingDue) {console.log({status:"INSUFFICIENT_FUNDS", change: []}); return {status:"INSUFFICIENT_FUNDS", change: []}};
if (remainingDue < 0.10 && (remainingNickel + remainingPenny) < remainingDue) {console.log({status:"INSUFFICIENT_FUNDS", change: []}); return {status:"INSUFFICIENT_FUNDS", change: []}};
if (remainingDue < 0.05 && remainingPenny < remainingDue) {console.log({status:"INSUFFICIENT_FUNDS", change: []}); return {status:"INSUFFICIENT_FUNDS", change: []}};

if (totalChange > remainingDue) { while (remainingDue >= 100) {

   //block fo 100+
    if (cid[8][1] >= 100) { arr[8][1] += 100; remainingDue -= 100; }
    else if (cid[7][1] >= 100) {cid[7][1] -= 100; arr[7][1] += 100; remainingDue -= 100; }
    else if (cid[6][1] >= 100) {cid[6][1] -= 100; arr[6][1] += 100; remainingDue -= 100; }
    else if (cid[5][1] >= 100) {cid[5][1] -= 100; arr[5][1] += 100; remainingDue -= 100; }
    else if (cid[4][1] >= 100) {cid[4][1] -= 100; arr[4][1] += 100; remainingDue -= 100; }
    else if (cid[3][1] >= 100) {cid[3][1] -= 100; arr[3][1] += 100; remainingDue -= 100; } 
    else if (cid[2][1] >= 100) {cid[2][1] -= 100; arr[2][1] += 100; remainingDue -= 100; }
    else if (cid[1][1] >= 100) {cid[1][1] -= 100; arr[1][1] += 100; remainingDue -= 100; }
    else if (cid[0][1] >= 100) {cid[0][1] -= 100; arr[0][1] += 100; remainingDue -= 100; };

    };

    //block for 20
    while (remainingDue >= 20 && remainingDue < 100) {

     if (cid[7][1] >= 20) {cid[7][1] -= 20; arr[7][1] += 20; remainingDue -= 20; }
    else if (cid[6][1] >= 20) {cid[6][1] -= 20.00; arr[6][1] += 20; remainingDue -= 20; }
    else if (cid[5][1] >= 20) {cid[5][1] -= 20.00; arr[5][1] += 20; remainingDue -= 20; }
    else if (cid[4][1] >= 20) {cid[4][1] -= 20.00; arr[4][1] += 20; remainingDue -= 20; }
    else if (cid[3][1] >= 20) {cid[3][1] -= 20.00; arr[3][1] += 20; remainingDue -= 20; }
    else if (cid[2][1] >= 20) {cid[2][1] -= 20.00; arr[2][1] += 20; remainingDue -= 20; }
    else if (cid[1][1] >= 20) {cid[1][1] -= 20.00; arr[1][1] += 20; remainingDue -= 20; }
    else if (cid[0][1] >= 20) {cid[0][1] -= 20.00; arr[0][1] += 20; remainingDue -= 20; };

    };


    //block for 10
    while (remainingDue >= 10 && remainingDue < 20) {

     if (cid[6][1] >= 10) {cid[6][1] -= 10; arr[6][1] += 10; remainingDue -= 10; }
    else if (cid[5][1] >= 10) {cid[5][1] -= 10; arr[5][1] += 10; remainingDue -= 10; }
    else if (cid[4][1] >= 10) {cid[4][1] -= 10; arr[4][1] += 10; remainingDue -= 10; }
    else if (cid[3][1] >= 10) {cid[3][1] -= 10; arr[3][1] += 10; remainingDue -= 10; }
    else if (cid[2][1] >= 10) {cid[2][1] -= 10; arr[2][1] += 10; remainingDue -= 10; }
    else if (cid[1][1] >= 10) {cid[1][1] -= 10; arr[1][1] += 10; remainingDue -= 10; }
    else if (cid[0][1] >= 10) {cid[0][1] -= 10; arr[0][1] += 10; remainingDue -= 10; };
};
    //block for 5

while (remainingDue >= 5 && remainingDue < 10) {

 	 if (cid[5][1] >= 5) {cid[5][1] -= 5; arr[5][1] += 5; remainingDue -= 5; }
 	else if (cid[4][1] >= 5) {cid[4][1] -= 5; arr[4][1] += 5; remainingDue -= 5; }
 	else if (cid[3][1] >= 5) {cid[3][1] -= 5; arr[3][1] += 5; remainingDue -= 5; }  
 	else if (cid[2][1] >= 5) {cid[2][1] -= 5; arr[2][1] += 5; remainingDue -= 5; }
 	else if (cid[1][1] >= 5) {cid[1][1] -= 5; arr[1][1] += 5; remainingDue -= 5; }
 	else if (cid[0][1] >= 5) {cid[0][1] -= 5; arr[0][1] += 5; remainingDue -= 5; };

};
 	//block for 1

while (remainingDue >= 1 && remainingDue < 5) {

 	 if (cid[4][1] >= 1) {cid[4][1] -= 1; arr[4][1] += 1; remainingDue -= 1}
 	else if (cid[3][1] >= 1) {cid[3][1] -= 1; arr[3][1] += 1; remainingDue -= 1}
 	else if (cid[2][1] >= 1) {cid[2][1] -= 1; arr[2][1] += 1; remainingDue -= 1}
 	else if (cid[1][1] >= 1) {cid[1][1] -= 1; arr[1][1] += 1; remainingDue -= 1}
 	else if (cid[0][1] >= 1) {cid[0][1] -= 1; arr[0][1] += 1; remainingDue -= 1};
};
 	// block for 0.25$

while (remainingDue >= 0.25 && remainingDue < 1) {

 	 if (cid[3][1] >= 0.25) {cid[3][1] -= 0.25; arr[3][1] += 0.25; remainingDue -= 0.25}
 	else if (cid[2][1] >= 0.25) {cid[2][1] -= 0.25; arr[2][1] += 0.25; remainingDue -= 0.25}
 	else if (cid[1][1] >= 0.25) {cid[1][1] -= 0.25; arr[1][1] += 0.25; remainingDue -= 0.25}
 	else if (cid[0][1] >= 0.25) {cid[0][1] -= 0.25; arr[0][1] += 0.25; remainingDue -= 0.25};
};
 	//block for 0.10$ 
while (remainingDue >= 0.10 && remainingDue < 0.25) {
 	 if (cid[2][1] >= 0.10) {cid[2][1] -= 0.10; arr[2][1] += 0.10; remainingDue -= 0.10}
 	else if (cid[1][1] >= 0.10) {cid[1][1] -= 0.10; arr[1][1] += 0.10; remainingDue -= 0.10}
 	else if (cid[0][1] >= 0.10) {arr[0][1] -= 0.10; arr[0][1] += 0.10; remainingDue -= 0.10};
 };

while (remainingDue >= 0.05 && remainingDue < 0.1) {
	if (cid[1][1] >= 0.05) {cid[2][1] -= 0.05; arr[2][1] += 0.05; remainingDue -= 0.05; }
 	else if (cid[0][1] >= 0.05) {cid[1][1] -= 0.05; arr[1][1] += 0.05; remainingDue -= 0.05; }
 	


};

while (remainingDue >= 0.009 && remainingDue < 0.05) {
	if (remainingDue >= 0.01 && cid[0][1] >= 0.01) {cid[0][1] -= 0.01; arr[0][1] += 0.01; remainingDue -= 0.01; }
	else if (remainingDue >= 0.009 && cid[0][1] >= 0.009) {cid[0][1] -= 0.001; arr[0][1] += 0.01; remainingDue -= 0.01; }

};


let arr1 = [];
for (var i = 0; i < arr.length; i++) {
    if (arr[i][1] > 0) {arr1.unshift(arr[i])}
};

console.log(arr1);

 console.log({status: "OPEN", change: arr1});

 return {status: "OPEN", change: arr1}
 	
   // console.log(arr, remainingDue);
 

  
  
  

/*else if (remainingDue === 0) {  console.log({status: "OPEN", change: arr})
  };*/


}

}