JavaScript Algorithms and Data Structures Projects - Cash Register

well i tried a way longer path to solving it with some help but it doesn’t output the wanted result can u help me out

function checkCashRegister(price, cash, cid) {
  let change = cash * 100 - price * 100;
  let cidTotal = 0;
  for (let elem of cid) {
    cidTotal += elem[1] * 100;
  }
  if (change > cidTotal) {
    return {status: "INSUFFICIENT_FUNDS", change: []};
  } else if(change === cidTotal) {
    return {status: "CLOSED", change: cid}
  } else {
    let result = [];
    cid = cid.reverse();
    let currencyUnit = {
      "ONE HUNDRED": 10000,
      "TWENTY": 2000,
      "TEN": 1000,
      "FIVE": 500,
      "ONE": 100,
      "QUARTER": 25,
      "DIME": 10,
      "NICKEL": 5,
      "PENNY": 1
  }
  for (let elem of cid) {
    let moneyHolder = [elem[0], 0];
    elem[1] = elem[1] * 100;
    while(change >= currencyUnit[elem[0]] && elem [1] > 0) {
      change -= currencyUnit[elem[0]];
      elem[1] -= currencyUnit[elem[0]];
      moneyHolder[1] += currencyUnit[elem[0]] 
      } 
      if (moneyHolder[1] > 0) {
        result.push(moneyHolder);
      } 
    }
    if (change > 0) {
      return {status: "INSUFFICIENT_FUNDS", change: []};
    }
    return {status: "OPEN", change: result}
  }
}

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

Your browser information:

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

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

Take a look at what function returns, it should clear up a bit what’s happening:

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

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

well Im sorry but i wish to get a little bit of clarification pls

Add the examples from my post at the end of your code. See what’s printed in the console and compare it with expected results - these are the failing test cases.

ok sure but its printing { status: ‘OPEN’, change: [ [ ‘QUARTER’, 50 ] ] } but it wants {status: "OPEN", change: [["QUARTER", 0.5]]} . There something that im not noticing, can u help me out

Take a closer look at the both numbers

ya i get there is deference in the beging of the data but i am not getting were the logic error is :pensive:

It’s not a logic error per se. The difference in result comes from using in it different units, so to speak.

im sry can u elaborate

Can you say, exactly, what the difference between

and

is?

well look at end the Frist one it says [ [ ‘QUARTER’, 50 ] ] } but the second one says [[“QUARTER”, 0.5]]} and the needed is the second one

What about the second failing test case? Do you see any pattern in the differences?

it prints this output “{ status: ‘OPEN’,
change:
[ [ ‘TWENTY’, 6000 ],
[ ‘TEN’, 2000 ],
[ ‘FIVE’, 1500 ],
[ ‘ONE’, 100 ],
[ ‘QUARTER’, 50 ],
[ ‘DIME’, 20 ],
[ ‘PENNY’, 4 ] ] }”

And how is that different than the requested output?

$6000 worth of twenty dollar bills is a lot of change :money_with_wings:

$9674 in change is a lot as well. Does that make sense?

[quote=“redietkebedewor, post:1, topic:632437”]
well i tried a way longer path to solving it with some help but it doesn’t output the wanted result can u help me out

MOD EDIT: SOLUTION REMOVED

The main changes:

  • Store currency units in an object for easy lookup
  • Use .reduce() to sum cid total instead of loop
  • Declare changeDue instead of change for clarity
  • Declare amount variable to track amount for each currency type
  • Divide by 100 when pushing to change array

Let me know if this helps explain a cleaner way to approach it!

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

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