I'm completely stuck. Please help!

Tell us what’s happening:

Hello everyone. I’ve managed to come this far entirely on my own without looking up a single answer, but I’m completely stuck on the final challenge, and it’s getting tough to not cheat and look up an answer and learn from it that way…

I know that the function should be counting the amount of times a certain dollar bill fits into the change that should be returned, and I know that the amount of times a certain dollar bill is allowed to be used depends on what is passed in with the cid. But how in god’s name do I manage this? There’s probably some way to do it with .map or .reduce but I’ve lost all sight of all possibilities. I’ve slept on this for a few days now and the urge to just cheat on this final one is overbearing.

Thank you so much for any possible hints.

  **Your code so far**

function checkCashRegister(price, cash, cid) {
 const index = [
  ["ONE HUNDRED", 100],
  ["TWENTY", 20],
  ["TEN", 10],
  ["FIVE", 5],
  ["ONE", 1],
  ["QUARTER", 0.25],
  ["DIME", 0.1],
  ["NICKEL", 0.05],
  ["PENNY", 0.01]
 ];

let change = cash - price;
let status;
let changeArr = [];
let changeObj = {};
let newCid = cid.reverse();

for (let i in index) {
  let indexValue = index[i][1];
  let indexUnit = index[i][0];
  let indexResource = newCid[i][1];
  let limit = indexResource / indexValue;
  let acc = Math.floor(change / indexValue);
  console.log(acc, limit);
  let realAcc = acc * indexValue;
  change -= realAcc;
  changeArr.push([indexUnit, acc]);
}
/* for (let i in newCid) {
  let cidValue = newCid[i][1];
  console.log("Cycling cid. The cidValue is " + cidValue);
  for (let j in index) {
    let indexUnit = index[j][0];
    let indexValue = index[j][1];
    let acc = Math.floor(change / indexValue);
    let realAcc = acc * indexValue;
    console.log("Real value is " + realAcc);
    if (realAcc <= cidValue && realAcc > 0 ) {
    change -= acc * indexValue;
    changeArr.push([indexUnit, realAcc]);
    }
  }
}*/


/* for (let i in cid) {
  for (let j in changeArr) {
    if (changeArr[j][0] == cid[i][0] && changeArr[j][1] == cid[i][1]) {
      status = "CLOSED";
    } else if (changeArr[j][0] == cid[i][0] && changeArr[j][1] > cid[i][1]) {
      status = "INSUFFICIENT_FUNDS";
    } else {
      status = "OPEN";
    }
  }
} */

changeObj.status = status;
changeObj.change = changeArr;
return changeObj;
}

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]]));
  **Your browser information:**

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

Challenge: Cash Register

Link to the challenge:

map and reduce are fancy ways to write loops, so lets just think about loops.

I like trying to make my code as simple as it can be. In this case, I would start by looking at what is the biggest unit of change I can give out. For example, if the cash is $20 and the price is $19.50, then the biggest unit of change I can give out is $0.25. How many quarters can I give out? Is there any change left to make after I give back as many quarters as I can? If so, what is the next largest unit of change you can give out? How may [dime/nickel/ect] can you give out? And so on…

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