The Cashier Problem

I am struggling on how to create a program to give adequate change to customers.The program should return the amount of notes and coins for the customer’s change.
Example: If the price is €3.75 and the paid amount is €50, then the client should receive €46.25 back in change.
The expected output should be:
2 x €20 // 2 twenty euro notes
1 x €5 // 1 five euro note
1 x €1 // 1 euro
1 x €0.2 // 1 twenty cent coin
1 x €0.05 // 1 five cent coin
Example: Price: €4.50, Paid amount: €20, Change: 15.50
Expected output:
1 x €10
1 x €5
1 x €0.5
Notes
Include outputs for exceptions e.g. price: €4, paid amount: €3. Any ideas on how to solve this Please?

This is where i have gotten to so far:

function checkCashRegister(price, cash, cid) {
    var change = cash * 100 - price * 100; // (In cents)
    var yourCash = {};
    var myCash = {};
    var i = 0;
  

const BILLS = [
    ["FIFTY", 50000],
    ["TWENTY", 2000],
    ["TEN", 1000],
    ["FIVE", 500],
    ["ONE", 100],
    ["QUARTER", 25],
    ["DIME", 10],
    ["NICKEL", 5],
    ["PENNY", 1]

    // No change? Done.
  if (change === 0) {
    return {
      status: "CLOSED",
      change: cid
    };
  }

  // Swap out array for an object
  cid.forEach(money => {
    myCash[money[0]] = parseInt(money[1] * 100);
  });

  // Give bills big -> small until change = 0 or fail
  while(i < BILLS.length && change > 0) {
    var billName = BILLS[i][0];
    var billValue = BILLS[i][1];

    // Can accept && give change
    if (change - billValue > 0 && myCash[billName] > 0) {
      // Set yourCash[billName]
      yourCash[billName] = 0;
      // Give as many of this bill as I can.
      while(change - billValue >= 0 && myCash[billName] > 0) {
        yourCash[billName] += billValue / 100;
        myCash[billName] = parseInt(myCash[billName] - billValue);
        change -= billValue;
      }
    }
    i++;
  }

  if (change === 0) {
    let hasMoney = false;

    Object.keys(myCash).forEach(key => {
      if (myCash[key] > 0) {
        hasMoney = true;
      }
    });

    if (hasMoney) {
      return {
        status: "OPEN",
        change: Object.keys(yourCash).map(key => {
        let obj = [key, yourCash[key]];
        console.log(JSON.stringify(obj));
        return obj;
      })};
    } else {
      console.log("NO Money Left...");
      return {
        status: "CLOSED",
        change: cid
      };
    }
  }
  return {
    status: "INSUFFICIENT_FUNDS",
    change: []
  }
}
checkcashRegister

Hi @Dad15 !

Welcome to the forum!

You will get more responses if you share your code.

Hi jwilkins.oboe,
Thanks for your response. I have updated the question with what i have done till where i am stuck.

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Also, just to clarify, this is for the last project for of javascript section right?
Because when, I originally read your post I thought this was for another solo project.

Yes. It is the last project for javascript.

1 Like

Ok cool.

I added the challenge as well so people can test out your code.

I have to run.
But you should receive way more responses now with the full context.

Good luck!
Happy coding!

const BILLS = [
    ["FIFTY", 50000],
    ["TWENTY", 2000],
    ["TEN", 1000],
    ["FIVE", 500],
    ["ONE", 100],
    ["QUARTER", 25],
    ["DIME", 10],
    ["NICKEL", 5],
    ["PENNY", 1]

    // No change? Done.
  if (change === 0) {

You are missing something here. BILLS is an array of arrays but it doesn’t end that way.

Thanks a lot for your response.

if you are writing how much each denomination is in pennies, your “FIFTY” has a value of 500$

Does it make a difference if i am using Euros. Because i don’t think so. Please any idea of a complete solution to my original question ?

const BILLS = [
[“FIFTY”, 500],

This is what you mean right?

now your fifty dollar bill value 5$ (500/100 = 5), so you will still get skewed results

1 Like

This is the most difficult problem I have faced so far. But it has its own charm: the cash compartments function like the “registers” in old assembler programming.

To solve it I had to write all the code using only addition and subtraction, and never ever using division or multiplication (except division or multiplication by 100).

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