I'm not sure what I'm doing wrong. Can someone take a look at this?

What’s happening:
My code returns a single-quoted string for the currency denominations(I don’t think a ten is called a denomination now that I think about it). I get a returned single quote, but when I use the google console, I get a double quote.

also, I could be using the wrong tool to get my solution.

Your code so far


function checkCashRegister(price, cash, cid) {
//variables first
let change = cash - price;
let registerArr = [];
let returnChange = 0;
let values = [100,20,10,5,1,.25,.1,.05,.01];
let valuesDenom = [100,20,10,5,1,.25,.1,.05,.01];
let registerTotal = 0;
let registerReturn = ["ONE HUNDRED","TWENTY","TEN","FIVE","DOLLAR","QUARTER","DIME","NICKEL", "PENNY"];
let resultingArr = []; 
let status = "";


//take register, place it in a variable 
//from highest value to lowest
let regValues = (arrayVals) => {
  registerArr.unshift(arrayVals[1]);
};

cid.forEach(regValues);

//determine if the change is in the register
let reduction = (total, num) => {
  return total + num;
}

registerTotal += registerArr.reduce(reduction);


//This function reduces values appropriately
let calculations = (value, index) =>{
  var currentDenomChange = 0;
  if(change > 0){
    if(change >= values[index]) {
      do{
      change = (change - values[index]).toFixed(2);
      currentDenomChange = currentDenomChange + values[index];
      returnChange = (((returnChange * 100) + (values[index]*100))/100).toFixed(2);
      //registerReturn.push([values[index]])
      
      


      //console.log(change + " and " + returnChange);
      }while(change >= values[index]);
      resultingArr.push([registerReturn[index],currentDenomChange]);
     // console.log(resultingArr);

    }
  }
}

//determines if the function will have
//equal, less or just enough money in the drawer
if (registerTotal > change){
      status = "OPEN";
  registerArr.forEach(calculations);
}else if (registerTotal == change){
      status = "CLOSED";
  registerArr.forEach(calculations);
  
} else {
  status = "INSUFFICIENT_FUNDS";
  resultingArr = [];
  //console.log("no money");
}

let finalReveal = new Map();
finalReveal['status'] = status;
finalReveal['change'] = resultingArr;
//console.log(finalReveal);

return finalReveal;
}
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]]));

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

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

checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

Your browser information:

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

Challenge: Cash Register

Link to the challenge:

1 Like

Hi, can you printscreen the things you’re getting? I don’t understand the problem :0

image
I believe this is the issue the OP was referencing.

But the single quotes aren’t causing the test script to fail. It’s this line here:

You’re returning a Map, which is similar to an Object but not the same. The test specifically wants an object.

1 Like

@nhcarrigan helped to find whats in my code. But shortly after writing this question out, I decided to keep at it.

I solved the problem for once! I remember sorta cheating my way thorugh the certification and feeling aweful. I came back to it and I solved it after taking 3 or 4 months away from FCC.

It was a sloppy ending, but I figured it out.

function checkCashRegister(price, cash, cid) {
    //variables first
    let change = cash - price;
    //console.log(change);
    let registerArr = [];
    let returnChange = 0;
    let values = [100, 20, 10, 5, 1, .25, .1, .05, .01];
    let valuesDenom = [100, 20, 10, 5, 1, .25, .1, .05, .01];
    let registerTotal = 0;
    let registerReturn = ["ONE HUNDRED", "TWENTY", "TEN", "FIVE", "ONE", "QUARTER", "DIME", "NICKEL", "PENNY"];
    let resultingArr = [];
    let finalReveal = {
        "status": "",
        "change": []
    };


    //take register, place it in a variable 
    //from highest value to lowest
    let regValues = (arrayVals) => {
        registerArr.unshift(arrayVals[1]);
    };

    cid.forEach(regValues);

    //determine if the change is in the register
    let reduction = (total, num) => {
        return total + num;
    }

    registerTotal += registerArr.reduce(reduction);
    //console.log(registerTotal);

    //This function reduces values appropriately
    let calculations = (value, index) => {
        var currentDenomChange = 0;
        if (change > 0) {
            if (change >= values[index]) {
                while (change >= values[index] && registerArr[index] >= values[index]) {
                    change = (((change * 100) - (values[index] * 100)) / 100).toFixed(2);
                    currentDenomChange = (((currentDenomChange * 100) + (values[index] * 100)) / 100);
                    returnChange = (((returnChange * 100) + (values[index] * 100)) / 100).toFixed(2);
                    registerArr[index] = (registerArr[index] - values[index]).toFixed(2);
                    console.log(registerArr[index])
                    if (registerArr.slice(index, ).reduce((total, num) => ((total * 100 + num * 100) / 100).toFixed(2)) == 0 && change != 0) {
                        finalReveal["status"] = "INSUFFICIENT_FUNDS";
                        resultingArr = [];
                        return finalReveal;
                    };
                };
                resultingArr.push([registerReturn[index], currentDenomChange]);
                // console.log(resultingArr);

            }
        }
    }


    let calculationsForExactChange = (value, index) => {
            //console.log(cid)
        }
        //determines if the function will have
        //equal, less or just enough money in the drawer
    if (registerTotal > change) {
        finalReveal["status"] = "OPEN";
        registerArr.forEach(calculations);
    } else if (registerTotal == change) {
        finalReveal["status"] = "CLOSED";
        resultingArr = cid;

    } else {
        finalReveal["status"] = "INSUFFICIENT_FUNDS";
        resultingArr = [];
    }


    finalReveal["change"] = resultingArr;
    //console.log(finalReveal);

    return finalReveal;
}

console
console.log(checkCashRegister(19.5, 20, [
    ["PENNY", 0.5],
    ["NICKEL", 0],
    ["DIME", 0],
    ["QUARTER", 0],
    ["ONE", 0],
    ["FIVE", 0],
    ["TEN", 0],
    ["TWENTY", 0],
    ["ONE HUNDRED", 0]
]));
type or paste code here
1 Like

So that’s how you got rid of floating points. I had a huge block of code dedicated to removing dumb floating point errors, which caused 1 dime + 2 dimes = 3.000000000004 ish dimes.

1 Like

Yep, JavaScript doesn’t like floating points. So you have to trick it or demand it to round things up and down.

My gut reaction was to multiply and divide by 100. And use toFixed() as a backup.

I was getting that anxious about not having to do this problem again.

There is a few stack exchange articles on this as well.


Just experiment with what works for a bit. Sometimes things would still get wonky. I’m still trying to figure out why.

1 Like