you have to format your code in a way that is readable. use prettier for vs code, if you do not want to do this, please find another option to format it. you can also use the online version prettier.io
How may I write the object in order to get the correct answer?
the object you wrote is fine. all your other code i do not like
ive formatted your code for better readability:
function checkCashRegister(price, cash, cid) {
var change = cash - price;
var money = [
["PENNY", 0.01],
["NICKEL", 0.05],
["DIME", 0.1],
["QUARTER", 0.25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
["ONE HUNDRED", 100]
];
var arr = [];
for (let i = 0; i < money.length; i++) {
let intMon = change / money[i][1];
if (Number.isInteger(intMon)) {
arr.push(intMon);
}
}
let minNumb = Math.min(...arr);
let isLargeNumber = element => element == minNumb;
let indexMin = arr.findIndex(isLargeNumber);
let st = ["OPEN", "CLOSED"];
if (cash > price) {
}
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
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]
]));
the last line of your code:
return { status: "INSUFFICIENT_FUNDS", change: [] };
you are always returning insufficient funds. that is incorrect. you should not do this.
if (cash > price) {
}
the above line serves no purpose. ive explained in spanish , maybe it was not clear. the cash is always greater than the price. no customer will attempt to pay you less. accept that as fact.
why have you not added up all the cash in your drawer as I showed you? you have to do this.
for (let i = 0; i < money.length; i++) {
let intMon = change / money[i][1];
if (Number.isInteger(intMon)) {
arr.push(intMon);
}
}
let minNumb = Math.min(...arr);
let isLargeNumber = element => element == minNumb;
let indexMin = arr.findIndex(isLargeNumber);
so… you are building an array of integers telling you how many times a certain denomination value can fit into the money you owe, and then your taking the smallest number in that arr and finding the index of the largest coin value that you can pay off your debt with. I would advise you not try to do it that way.
all you will find out using that way of thinking is that you are able to theoretically pay off your 50 cent debt with 2 quarters, and as youve found out that in the same vain you can also pay it with 50 pennies, or 10 nickels, or five dimes… so yes you are choosing the largest value, but how do you even know you have two quarters in your possession? what if the debt is 60 cents? what will you do then? you need to consider these things.
you are not checking how much monies you actually have of a specific coin, no place in your code are you adding up all your money in your drawer, no place in your code are you subtracting frow what you actually have in your possession while making your payouts while at the same time subtracting from your debt to the customer