Tell us what’s happening:
My algo. works only on first case and case where there’s not enough money in cashregister. My question is: How to “jump” to next value and add denomination and sum of that denomination/banknote i.e “quarter”, 0.5 for example using already existing (my)code. I either don’t see obvious, or just tired since i’m on this for couple of days now from noon to 2-3am(today: 3:35am) … As you can see, in commented code, what i already tried.
Edit1: Althrough My english is rather good for non english speaker, due to exhaustion, i’m may sound as neanderthal trying to explain string theory to an alien from alternate universe. So, feel
to ask for any clarification you may need.
Update No.1(code): Update No.2(code): It works now
Your code so far
// Create an object which hold the denominations and their values
function checkCashRegister(price, cash, cid) {
let thirdParCid = {
names: [],
vals: []
};
thirdParCid.names=cid.map(x => x[0]); //imena novcanica u kasi(banknote)/names of bill-coins
thirdParCid.vals=cid.map(x => x[1]); //kolicina novcanica po banknoti u kasi//sum of bills per banknote
var cashRegisterSum = thirdParCid.vals.reduce((x,y)=>x+y);//ukupna kolicina novca u kasi.//complete sum of all money in cash-register
var obj1 = {
banknote: [...thirdParCid.names],//Imena banknota/names of bill-coins
initial: [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100],//Vrednost banknota/values of bill-coins
sum: [...thirdParCid.vals],//suma novcanica po banknoti u kasi/sum of bills per banknote
num: [],//kolicina novcanica po banknoti u kasi/complete sum of all money in cash-register per banknote
kusur: (cash-price)//kusur/change
};
obj1.num = thirdParCid.vals.map((n,i) => n / obj1.initial[i]);
obj1.obj1Len = obj1.banknote.length-1;//Duzina niza objekta./how many banknotes are..
obj1.highestDivisibleName = obj1.banknote[obj1.obj1Len];//Ime najvece pojedinacne banknote koja se moze dati za kusur u objektu./name of highest value banknote
obj1.highestDivisibleAmmount = obj1.initial[obj1.obj1Len];//Vrednost najvece pojedinacne banknote koja se moze dati za kusur u objektu./value of highest value banknote
obj1.containers = (obj1.initial.filter(x => x<=obj1.kusur)).reverse();//novcanice koje sadrze u kusuru/banknotes values that can be in change. Example: change: 0.5$, banknote: 0.25 and all lower then 0.25.
var i=0;
var change = {
status: null,
change: []
};
var ind=0;
var denomination="";
var temp=0;
var arr1 = [];
if(obj1.kusur>cashRegisterSum){
change = {status: "INSUFFICIENT_FUNDS", change: []};
return change;
}
if(obj1.kusur<cashRegisterSum){
while(i<obj1.containers.length || obj1.kusur !== 0){
var ttt=Math.floor(obj1.kusur/obj1.containers[i]);
if(obj1.kusur>obj1.containers[i]){
for(var j=0;j<ttt;j++){
ind = obj1.initial.indexOf(obj1.containers[i]);
denomination = obj1.banknote[ind];
obj1.kusur -= obj1.containers[i];
temp += obj1.containers[i];
obj1.kusur= parseFloat(obj1.kusur.toFixed(2));
temp = parseFloat(temp.toFixed(2));
console.log(obj1.kusur+" => "+temp+" => "+obj1.containers[i]+" => "+i);
}
}
else{
i++;
}
}
}
//console.log(change);
}
// test here
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]]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36
.
Link to the challenge: