Cash Register - Anyone help me this? thanks so much

Cash Register - Anyone help me this? thanks so much
0.0 0

#1

Tell us what’s happening:

Your code so far


function checkCashRegister(price, cash, cid) {
  var change;
  // Here is your change, ma'am.
  return change;
}

// Example cash-in-drawer array:
// [["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]]);

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/


Cash Register - anyone help me this? thanks
#2

You have not written any code yet.

Have you written an algorithm yet? If not, do that first and walk through the test cases to make sure your written algorithm would produce the correct results. Then, begin to code your solution. If you get stuck on the algorithm or the code, post either of them and we will give you some guidance.


#3

yes, I had code but when I run test it showed: 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]]) should return an object.
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]]) should return {status: “OPEN”, change: [[“QUARTER”, 0.5]]}.
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]]) should return {status: “OPEN”, change: [[“TWENTY”, 60], [“TEN”, 20], [“FIVE”, 15], [“ONE”, 1], [“QUARTER”, 0.5], [“DIME”, 0.2], [“PENNY”, 0.04]]}.
checkCashRegister(19.5, 20, [[“PENNY”, 0.01], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]) should return {status: “INSUFFICIENT_FUNDS”, change: []}.
checkCashRegister(19.5, 20, [[“PENNY”, 0.01], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 1], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]) should return {status: “INSUFFICIENT_FUNDS”, change: []}.
checkCashRegister(19.5, 20, [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]) should return {status: “CLOSED”, change: [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]}.


#4

Tell us what’s happening:

Your code so far


var money = [
    { name: 'ONE HUNDRED', value: 100.00 },
    { name: 'TWENTY', value: 20.00 },
    { name: 'TEN', value: 10.00 },
    { name: 'FIVE', value: 5.00 },
    { name: 'ONE', value: 1.00 },
    { name: 'QUARTER', value: 0.25 },
    { name: 'DIME', value: 0.10 },
    { name: 'NICKEL', value: 0.05 },
    { name: 'PENNY', value: 0.01 }
];

function checkCashRegister(price, cash, cid) {

    // calculate how much change we owe the customer
    var change = cash - price,
        till = cid.reduce(function (a, b) {
            return a + b[1];
        }, 0.0).toFixed(2);
    if (till < change) {
        return "INSUFFICIENT_FUNDS";
    } else if (till == change) {
        return "CLOSED";
    }
    cid = cid.reverse();

    var result = money.reduce(function (acc, next, index) {
        if (change >= next.value) {
            var currentValue = 0.0;
            while (change >= next.value && cid[index][1] >= next.value) {
                currentValue += next.value;
                change -= next.value;
                change = Math.round(change * 100) / 100;
                cid[index][1] -= next.value;
            }
            acc.push([next.name], currentValue);
            return acc;
        }
        else {
            return acc;
        }
    }, []);
    return result.length > 0 && change === 0 ? result : "Insufficient Funds";
}
checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/


#5

The checkCashRegister() function should always return an object with a status key and a change key.

You are returning an array or a string depending on the following condition in your return statement:

result.length > 0 && change === 0