0

#1

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

``````
// 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]]);

``````

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`.

And on image i provided for some strange reason, it refuses further subtraction, hence it goes in a infinite loop.

#2

While I can see why you tried to find number of bills/coins, I think you’d be better off just subtracting off from the values given you. What happens is that then you’re dividing to find the numbers of bills, and then multiplying to get back to a total value, which kinda defeats the purpose of dividing it in the first place.

That being said, I am confused on your use of the obj1.containers. Why would it be necessary?

#3

Oh and before I forget. Thank the lord almighty (or whatever you believe in/not) that you commented your code. Much appreciated!

#4

What is in obj1.containers? Can you console.log it?
I believe you should start from the largest bill or coin that is less than the current value of change or ‘kusur’ and give us much bills or coins as possible. What I did is, I try to find integer value of how much coins/bills I am able to give for change, starting from the largest bill or coin. I do not calculate it all at once, I just iterate through the loop, and while substracting from change, some bills/coins are skipped because they become larger than the change remaining

#5

There are not. But i like to know them upfront instead to look for them every time i need it.

#6

Bill that are less in value in their value then change. Example: Change 0.5\$ , first values is quarter(0.25\$). All values after that are lesser then 0.25. And i did that. All of those values are in “containers”.

#7

Guess people are just missing what i actually asked: In condition where is change less than cash in register, when you check in first denomination and substract how many until you get something that is less then that denomination, and after you have to jump to next denom. and continue substracting and process rinse-and-repeat until you hit change==0, is question: What is wrong in my condition so i can rectify it to jump jump to next denom? And when to push arr1 to change.change object?

#8

I know I didn’t your question, but it’s a little hard for me at least to understand why you have certain variables, as there is no need to keep track of that.

The way I see it, if I were a cash register, I’d draw from the highest tender first. Since the value is given to you, just take a bill, subtract 20 from the total value amount, and if the tender value is more than the change, take another. Otherwise, go to the next lowest tender. Repeat with drawing the tender.

#9

Example: If you have change= 0.56\$ , you’ll take from cash reg. -0.25, -0.25\$(quarters), then you jump to next denom… Thus being said, one has to know how much per “taking cycle” can take from certain denomination. From [“quarters”] you take 0.25\$ until you’re left with amount smaller then that step. So, you Have to know what is step for [“quarters”] upfront.

#10

Okay. But I think these lines need correction

``````else{
ind = obj1.initial.indexOf(obj1.containers[i]);
denomination = obj1.banknote[ind];
obj1.kusur -= obj1.containers[i];
temp += obj1.containers[i];

}
``````

I may be wrong, but here to me it looks like you are substructing from ‘kusur’ (change) only the amount of one bill.

#11

You are on the right road. Na dobrom si putu
But I do not think you have to know it upfront, just calculate it inside the loop everytime…

#12

Please do not change a topic category once a moderator has set it for you. I have changed the category back to “JavaScript” as your question(s) all relate to JavaScript.

Thank you.

#13

Ok. I can see where you’re going with that.

If that’s the case, why not just subtract from thirdParCid. You’ve made a copy of cid, but you don’t do very much with it. To keep track of how much you’re returning, add to an empty array. I just feel that the use of that containers variable unnecessarily complicates things.

One other thing I saw. You don’t have a “CLOSED” case, but I think you’re waiting to get “OPEN” working.

#14

Yes. Let’s make make one thing done, then on the rest. Since only difference between open and closed is that there’s no money left in cashreg after returning change. I.e change==cashreg_money. For closing and insufficient fund you don’t even need fancy calculations. If(change==cashInReg){//closed, change or cashInReg they’re are same thing here…}, if(change>cashInReg){//insufficient money}

#15

Its automatically put “javascript” tag on it when i first posted. Didn’t noticed it until later, so i change it to “help”. I will not change it any further.

#16

Вала баш … Of course i don’t have to, but it’s easier to do it earlier and upfront, then to remember it every time i need it. I just like to have all ingredients beforehand. And you know why people telling me this, it’s because efficiency. But they don’t realize, i don’t know this algo yet. Until i cross the road(whole), after i have working example whom addresses all possible combinations, then and only then efficiency, readability and optimization will be of any concern.

#17

Actually, the system marked it as Help by default, then @ArielLeslie changed it from Help to JavaScript, and then later you changed it back to Help.

We have put a request in that the system classify all the JavaScript algorithm challenges as JavaScript by default so it starts with the correct category. Hopefully it will be implemented in the next day or so. Thank you for understanding.

#18

That is correct. But here, you already have all ingredients in the cash register. And in this case upfront calculation means you are maybe doing more than is necessary.
Anyway, I have no worries for you, I see you have the knowledge. My oppinoin is that you overestimated this challenge