Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I have no idea why its not working. it is malfunctioning when returning what change is sent

Your code so far

let price = 19.5;
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]
];
function calculateCashInDrawer(cid) {
        let cashInDrawer = 0;
        for (const [denomination, amount] of cid) {
            switch (denomination) {
                case 'PENNY':
                    cashInDrawer += amount * 0.01;
                    break;
                case 'NICKEL':
                    cashInDrawer += amount * 0.05;
                    break;
                case 'DIME':
                    cashInDrawer += amount * 0.10;
                    break;
                case 'QUARTER':
                    cashInDrawer += amount * 0.25;
                    break;
                case 'ONE':
                    cashInDrawer += amount;
                    break;
                case 'FIVE':
                    cashInDrawer += amount * 5;
                    break;
                case 'TEN':
                    cashInDrawer += amount * 10;
                    break;
                case 'TWENTY':
                    cashInDrawer += amount * 20;
                    break;
                case 'ONE HUNDRED':
                    cashInDrawer += amount * 100;
                    break;
            }
        }
        return cashInDrawer;
    }
function changeBills(changeDue) {
        let bills = [];
        while (changeDue > 0) {
            if (changeDue >= 100) {
                bills.push(['One Hundred', '$' + (changeDue/100)*100]);
                changeDue = changeDue % 100;
            } else if (changeDue >= 20) {
                bills.push(['Twenty', '$' + (changeDue/20)*20]);
                changeDue = changeDue % 20;
            } else if (changeDue >= 10) {
                bills.push(['Ten', '$' + (changeDue/10)*10]);
                changeDue = changeDue % 10;
            } else if (changeDue >= 5) {
                bills.push(['Five', '$' + (changeDue/5)*5]);
                changeDue = changeDue % 5;
            } else if (changeDue >= 1) {
                bills.push(['One', '$' + (changeDue/1)*1]);
                changeDue = changeDue % 1;
            }
            else if (changeDue >= 0.25) {
                bills.push(['Quarter', '$' + (changeDue/0.25)*0.25]);
                changeDue = changeDue % 0.25;
            }
            else if (changeDue >= 0.1) {
                bills.push(['Dime', '$' + (changeDue/0.1)*0.1]);
                changeDue = changeDue % 0.1;
            }
            else if (changeDue >= 0.05) {
                bills.push(['Nickel', '$' + (changeDue/0.05)*0.05]);
                changeDue = changeDue % 0.05;
            }
            else if (changeDue >= 0.01) {
                bills.push(['Penny', '$' + (changeDue/0.01)*0.01]);
                changeDue = changeDue % 0.01;
            }
            else {
                break;
            }
        }
        return bills;
      }

function purchase(){
    let cash = parseFloat(document.getElementById('cash').value);
    let cashInDrawer= calculateCashInDrawer(cid);
    
    if(price>cash){
        alert("Customer does not have enough money to purchase the item");
        console.log("Customer does not have enough money to purchase the item");
    } else if(price===cash){
        document.getElementById('change-due').innerHTML = 'No change due - customer paid with exact cash';
        console.log("No change due - customer paid with exact cash");
    }
    else{
        let changeDue=cash-price;
        if (cashInDrawer<changeDue){
            return "Status: INSUFFICIENT_FUNDS";
        } else if (cashInDrawer===changeDue){
            return "Status: CLOSED";
        }else{
            let bills = changeBills(changeDue);
            document.getElementById('change-due').innerHTML = "Status:OPEN "+bills;
            return "Status:OPEN "+bills;
            
        }
    }

}



Purchase

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

please describe the issue in more detail than this one line.

why are you doing this? you already have the value in dollars in a certain denomination

your first method is completely ****** you over, i genuinely dont have the time managment to look farther than that

1st, all you literally have to do for the same exact wanted result is run a basic reduce sum method on your cid array, and simply add a [1] for each sum variable (or whatever else you would like to name the variable that is going to be representing the array elements that are being iterated over) that would be added, for the exact same outcome of adding the complete cash value of “cid” (the given register drawer)

that would look like

cid.reduce( (acc, sum) => acc += sum[1], 0)

.reduce() is a built in method for every array in javascript and based on the first parameter which is going to be a personalized function, that based on its logic will compound every element of the array starting with the index represnted by the second parameter

and secondly, im pretty sure that the way you are defining & using your variables of the method i mention, you are accessing the numerical cash value sum for each denomination with the “amount” variable, because that is what the cid variable is going to be, but then you’re accounting for that by multiplyng it by the denomination face value,

so for example your switch case for hundreds, with the current cid array you would be returning 10,000 to the cash drawer since you’re multiplying the sum cash value total of hundreds in the drawer (100, because there’s logically only one bill for this version of “cid”) by the monetary value of the currency.

its confusing the way this homework is set up because the arrays are meant to be read as [currencytype, cashTotalOfThatCurrencyInDrawer] but reading something like [“FIVE”, 20], the brain can easily interpret that is meaning $100 when in this program it has to mean $20, you will have to account for the quantity of the units it implies on your own if thats information you want documented, but also remember there are array methods that will help you find simpler approaches too

up to this point in the entire JS track, findIndex(), .map(), .filter(), and .reduce() are Array methods I would require myself to understand how to use, and knowing the difference between implied returns in arrow functions and standardly defined arrow functions with braces, before attempting to complete or solve this homework, you may not even find use for one or any of these in the solution you come up with but its baseline assumption of what you already understand by submitting & passing this homework in any scenario

& i also find the AI websites like Gemini or ChatGPT can give you quick answers to programming questions if you want to have a more active dialogue in the online learning process