Stuck at Cash Register.. Help if u can

I think im close but I can’t get the right format at the end…

let price = 14.5;
let cash = 20;
let change = cash-price;
console.log("Must give back : " + change + "\n");

let cid = [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]];

let obj ={
    "PENNY" : 0.01,
    "NICKEL" : 0.05,
    "DIME" : 0.1,
    "QUARTER" : 0.25,
    "ONE" : 1,
    "FIVE" : 5,
    "TEN" : 10,
    "TWENTY" : 20,
    "ONE HUNDRED" : 100,
    0.01:"PENNY",
    0.05:"NICKEL",
    0.1:"DIME",
    0.25:"QUARTER",
    1:"ONE",
    5:"FIVE",
    10:"TEN",
    20:"TWENTY",
    100:"ONE HUNDRED"
}
let cid_total = 0;
for(let i = 0;i<cid.length;i++){
    cid_total += cid[i][1];
}
let ar = [];
for(let i=cid.length-1;i>=0;i--){
    ar.push(obj[cid[i][0]]);
}
let ar_rev = ar.slice().reverse();

if(cid_total<change){
    console.log("Insufficient_funds");
}
else{

    let str = "";
    for(let i = 0;i<ar.length;i++){
    if(ar[i]<change){
        var res = Math.floor(change/ar[i]);
        var useful_index = ar_rev.indexOf(obj[obj[[ar[i]]]]);
        var cash_needed = res * obj[obj[[ar[i]]]];
        var cash_available = cid[useful_index][1];
        
        if(cash_needed > cid[useful_index][1]){
            continue;
        }

        var hi ={
            status : "OPEN",
            change : [obj[[ar[i]]],res*obj[obj[[ar[i]]]]]
        };
        console.log(hi);
        change = ((change-(res*ar[i])).toFixed(2));
        continue;
    }
} 


// let mi = ("{status: \"OPEN\", change: "+ str+"}");
// console.log(mi);

}

can you please give all your code including the function definition

also please share a link to the challenge

This is my code… I run it on an online editor and didn’t use functions. I just stored the values to variables for testing. Thank you.

I have no idea how to test your code if you don’t use a function as per the project

Is this ok?

function checkCashRegister(price, cash, cid) {
  let change = cash-price;
  let obj ={
    "PENNY" : 0.01,
    "NICKEL" : 0.05,
    "DIME" : 0.1,
    "QUARTER" : 0.25,
    "ONE" : 1,
    "FIVE" : 5,
    "TEN" : 10,
    "TWENTY" : 20,
    "ONE HUNDRED" : 100,
    0.01:"PENNY",
    0.05:"NICKEL",
    0.1:"DIME",
    0.25:"QUARTER",
    1:"ONE",
    5:"FIVE",
    10:"TEN",
    20:"TWENTY",
    100:"ONE HUNDRED"
} 
  let cid_total = 0;
  for(let i=0;i<cid.length;i++){
    cid_total += cid[i][1];
  }
  let ar=[];
  for(let i=cid.length-1;i>=0;i--){
    ar.push(obj[cid[i][0]]);
  }
  let ar_rev = ar.slice().reverse();

  if(cid_total<change){
    return "{status: \"INSUFFICIENT_FUNDS\", change: []}";
  }
  else{
    let str = "";
    for(let i=0;i<ar.length;i++){
      if(ar[i]<change){
        var res = Math.floor(change/ar[i]);
        var useful_index = ar_rev.indexOf(obj[obj[[ar[i]]]]);
        var cash_needed = res*obj[obj[[ar[i]]]];
        var cash_available = cid[useful_index][1];

        if(cash_needed>cid[useful_index][1]){
          continue;
        }
        var hi = {
          status:"OPEN",
          change:[obj[[ar[i]]],res*obj[obj[[ar[i]]]]]
        };
        
        change = ((change-(res*ar[i])).toFixed(2));

        continue;
        
      }

    }
    return hi;
  }
  
}

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 think you have a bigger problem than the format of your result.

Try adding this after your existing console.log statement:

console.log(checkCashRegister(75, 100, [["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 10], ["FIVE", 10], ["TEN", 10], ["TWENTY", 0], ["ONE HUNDRED", 0]]));

You need to return $25 change and you have $30 in the cash register.
Your code should return:

{ status: 'OPEN',   change: [ [ 'TEN', 10 ], [ 'FIVE', 10 ], [ 'ONE', 5 ] ] }

But when I run that with the code you posted, I get a different result.

Think about what will happen when your loop arrives at the 'TEN' value:

  • your test if(ar[i]<change) will return true
  • $10 isn’t enough to provide all the change you need but it’s a good start :slight_smile:
  • what should your code do in this case?

Then try this:

console.log(checkCashRegister(75, 100, [["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 25], ["FIVE", 0], ["TEN", 0], ["TWENTY", 40], ["ONE HUNDRED", 0]]));

You need to return $25 change and this time you have $65 in the cash register.
Your code should return:

{ status: 'OPEN', change: [ [ 'TWENTY', 20 ], [ 'ONE', 5 ] ] }

Again, when I run that with the code you posted, I get a different result.

Think about what will happen when your loop arrives at the 'TWENTY' value:

  • your test if(ar[i]<change) will return false
  • but you can start building your change amount by using $20 of the $40 in this array element
  • what should your code do in this case?

If you can figure all that out, then I think you’ll be a step closer to a working solution.
(And your result format problem will probably also be solved.)

Good luck!

Thanks for taking the time. I thought my logic was right and when trying it online I got “correct” answers… i’ll try building it from scratch again. Thank you.

1 Like

This is my last attempt… I don’t think it is worth spending more time on this… Every idea is welcome. :confused:

function checkCashRegister(price, cash, cid) {
  let obj ={
    "PENNY" : 0.01,
    "NICKEL" : 0.05,
    "DIME" : 0.1,
    "QUARTER" : 0.25,
    "ONE" : 1,
    "FIVE" : 5,
    "TEN" : 10,
    "TWENTY" : 20,
    "ONE HUNDRED" : 100,
}

  let obj2 ={
    0.01:"PENNY",
    0.05:"NICKEL",
    0.1:"DIME",
    0.25:"QUARTER",
    1:"ONE",
    5:"FIVE",
    10:"TEN",
    20:"TWENTY",
    100:"ONE HUNDRED"
  }
  let cid_ar = [];
  for(let i =0;i < 9;i++){
    cid_ar.push(cid[i][1]);
  }

  let nums_ar = Object.values(obj);
  let ar_rev = nums_ar.slice().reverse();
  console.log(ar_rev);
  let change = (cash-price);
  console.log("must return: " + change);

  let total_cash = 0;
  for(let i = 0;i<cid.length;i++){
    total_cash+=(cid[i][1]);
  }
  console.log("total cash: " + total_cash);

  if(total_cash<change){
    let not_enough ={
      status:"INSUFFICIENT_FUNDS",
      change:[]
    }
    return not_enough;

  }
  else if(total_cash==change){
    let closed = {
      status: "CLOSED",
      change: cid
    }
    return closed;
  }
  else{
    for(let i=0;i<ar_rev.length;i++){
      if(ar_rev[i]<change){
        var xwraei = Math.floor(change/ar_rev[i]);
        let arindex = nums_ar.indexOf(ar_rev[i]);
        let cash_available = cid[arindex][1];

        // if(xwraei*ar_rev[i]>cash_available){
        //   change = change - cash_available;
        // }else{
          change = ((change-(xwraei*ar_rev[i])));
        // } 
        let met = xwraei*ar_rev[i];
        
        if(met>cash_available){
          met = cash_available;
        }
        var finito = {
          status : "OPEN",
          change : [[obj2[ar_rev[i]],(met)]]
        };
        console.log(finito);
        continue;
      }      
    }
  }

}

console.log(checkCashRegister(12.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 think you are at least missing a return statement, your else part doesn’t return anything

Don’t give up!
I think you can get through this.

The biggest problem with the code that you have so far is that everything is happening inside your for loop.

Your finito variable only exists for that iteration of the loop.

Remember, the change value of of the object that you have to return from this function is a nested array .

In each iteration of your for loop, if you can make part of your change from that currency denomination, then you can create one element of that nested array e.g. [ 'FIVE', 5 ].

Can you figure out how have a nested array, containing all the change elements you need, after your for loop finishes ?

(Take a look at “Iterate Through All an Array’s Items Using For Loops” in the “Basic Data Structures” part of the course.)

If you can get that far, then you need to construct and return an object after your for loop terminates (as Ilenia pointed out).

At which point you’ll be a lot closer to a solution that works.

1 Like

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