Cash Register, someOne should help me on this I cannot figure out why it won't pass

Tell us what’s happening:

const REGISTER_STATUS = { closed: "CLOSED", insufficientFunds: "INSUFFICIENT_FUNDS", open: "OPEN" };

function checkCashRegister(price, cash, cid) {
    
    // Here is your change, ma'am.
    let cashRegister = {status: "", change: cid};
    const changeNeeded = parseFloat(cash - price).toFixed(2);
    const changeAvailable = getTotalCashRegisterChange(cid);
    cashRegister.status = getTotalCashRegisterStatus(changeNeeded, changeAvailable);
   



    if(cashRegister.status === REGISTER_STATUS.insufficientFunds) {
        cashRegister.change = [];

        return cashRegister;
    }

    cashRegister.change = getCustomerChange(changeNeeded, cid);
    

    if(changeNeeded > getTotalCashRegisterChange(cashRegister.change)){
        cashRegister.status = REGISTER_STATUS.insufficientFunds;
        cashRegister.change = [];
    }
    if(cashRegister.status === REGISTER_STATUS.closed){
        cashRegister.change = [...cid]
    }
    return cashRegister;

    //console.log(cashRegister);
}

function getCustomerChange(changeNeeded, changeInDrawer){
    const change = [];
    const currencyDictionary = {
        "PENNY": 0.01,
        "NICKEL": 0.05,
        "DIME": 0.10,
        "QUARTER": 0.25,
        "ONE": 1.00,
        "FIVE": 5.00,
        "TEN": 10,
        "TWENTY": 20,
        "ONE HUNDRED": 100
    };
    for(let i = changeInDrawer.length - 1; i >= 0; i--){
        const coinName = changeInDrawer[i][0];
        const coinTotal = changeInDrawer[i][1];
        const coinValue = currencyDictionary[coinName];
        let coinAmount = (coinTotal / coinValue).toFixed(2);
        let coinsToReturn = 0;

        while (changeNeeded >= coinValue && coinAmount > 0){
            changeNeeded -= coinValue;
            changeNeeded = changeNeeded.toFixed(2);
            coinAmount--;
            coinsToReturn++;
        }
        
        if(coinsToReturn > 0){
            change.push([coinName, coinsToReturn * coinValue]);
        }
    }
    return change;
}

function getTotalCashRegisterStatus(changeNeeded, changeAvailable){
    if(Number(changeNeeded) > Number(changeAvailable)) {
        return REGISTER_STATUS.insufficientFunds;
    }
    if(Number(changeNeeded) < Number(changeAvailable)) {
        return REGISTER_STATUS.open;
    }
    return REGISTER_STATUS.closed;
}

function getTotalCashRegisterChange(changeInDrawer){
    let total = 0;
    for(let change of changeInDrawer){
        let changeValue = change[1];
        total += changeValue;
    }
    return total.toFixed(2);
}

Your code so far


const REGISTER_STATUS = { closed: "CLOSED", insufficientFunds: "INSUFFICIENT_FUNDS", open: "OPEN" };

function checkCashRegister(price, cash, cid) {
    
    // Here is your change, ma'am.
    let cashRegister = {status: "", change: cid};
    const changeNeeded = parseFloat(cash - price).toFixed(2);
    const changeAvailable = getTotalCashRegisterChange(cid);
    cashRegister.status = getTotalCashRegisterStatus(changeNeeded, changeAvailable);
   



    if(cashRegister.status === REGISTER_STATUS.insufficientFunds) {
        cashRegister.change = [];

        return cashRegister;
    }

    cashRegister.change = getCustomerChange(changeNeeded, cid);
    

    if(changeNeeded > getTotalCashRegisterChange(cashRegister.change)){
        cashRegister.status = REGISTER_STATUS.insufficientFunds;
        cashRegister.change = [];
    }

    if(cashRegister.status === REGISTER_STATUS.closed){
        cashRegister.change = [...cid]
    }
    return cashRegister;

    //console.log(cashRegister);
}

function getCustomerChange(changeNeeded, changeInDrawer){
    const change = [];
    const currencyDictionary = {
        "PENNY": 1.01,
        "NICKEL": 0
[details="Summary"]
This text will be hidden
[/details]
.05,
        "DIME": 3.1,
        "QUARTER": 0.25,
        "ONE": 1.00,
        "FIVE": 5.00,
        "TEN": 20,
        "TWENTY": 60,
        "ONE HUNDRED": 100
    };
    for(let i = changeInDrawer.length - 1; i >= 0; i--){
        const coinName = changeInDrawer[i][0];
        const coinTotal = changeInDrawer[i][1];
        const coinValue = currencyDictionary[coinName];
        let coinAmount = (coinTotal / coinValue).toFixed(2);
        let coinsToReturn = 0;

        while (changeNeeded >= coinValue && coinAmount > 0){
            changeNeeded -= coinValue;
            changeNeeded = changeNeeded.toFixed(2);
            coinAmount--;
            coinsToReturn++;
        }
        
        if(coinsToReturn > 0){
            change.push([coinName, coinsToReturn * coinValue]);
        }
    }
    return change;
}

function getTotalCashRegisterStatus(changeNeeded, changeAvailable){
    if(Number(changeNeeded) > Number(changeAvailable)) {
        return REGISTER_STATUS.insufficientFunds;
    }
    if(Number(changeNeeded) < Number(changeAvailable)) {
        return REGISTER_STATUS.open;
    }
    return REGISTER_STATUS.closed;
}

function getTotalCashRegisterChange(changeInDrawer){
    let total = 0;
    for(let change of changeInDrawer){
        let changeValue = change[1];
        total += changeValue;
    }
    return total.toFixed(2);
}

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 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36.

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

You haven’t actually asked a question. I did test your code, and in both of the failing tests, your code is returning:

{"status":"INSUFFICIENT_FUNDS","change":[]}

In the process of debugging, I’m coming up with a sizeable question: what is the purpose of your currencyDictionary inside your getCustomerChange() function?

At no point do you tell your code how to convert a nickel, or dime, or five dollars, to pennies.

Lacking that, I’m sort of suprised by how many tests you PASSED, rather than that you failed two.

EDIT that currencyDictionary is clearly the conversion array, but the value for penny and for dime is what’s throwing you off, I think.

Oh Well i have it figured out! Thank you

1 Like

Excellent. Glad it worked out.

1 Like