I´m working on the cash register algorithm challenge. And I´m still stuck in the same problem that i was 2 days ago. I build a for loop with some conditionals inside to make it work, but somewhere inside the conditionals, I get irremediably lost and I don´t know how to make it work.
I have tried to erase it and build it different ways but I always come up with the same approach and I can´t get my head around how to “debug” this:
…note: “fromthere” means from where it should start counting backwards. Don´t worry about this.
identicalObject = [["PENNY", 0],["NICKEL", 0],["DIME", 0],["QUARTER", 0],["ONE", 0],["FIVE", 0],["TEN", 0],["TWENTY", 0],["ONE HUNDRED", 0]]
// toReturn = 96.74
var mustbethesame = 0
var typeofCurrency = [0.01,0.05,0.1,0.25,1,5,10,20,100]
// cid = [1.05,2.05,3.1,4.25,90,55,20,60,100]
for (var i=fromthere; i > -1; i--){
if (typeofCurrency[i] > toReturn){
1==1 //continue to the next one
}
else {
let counter = 0;
counter = cid[i][1] / typeofCurrency[i]
for (var j=0; j<counter;j++){
mustbethesame += typeofCurrency[i]
if (mustbethesame < toReturn){
identicalObject[i][1] += typeofCurrency[i]
}
else {
1==1 // continue to the next one
}
}
}
}
Basically, apart from an empty array what the for loop will be filling with values, I create an empty variable which its supposed to serve as “blocker”. If this variable gets bigger than the value to return, then the loop doesn´t add the last value and go to the next currency (or at least its what it supposed to do)
Here´s what it returns:
console.log(identicalObject) // returns
[ 'PENNY', 0 ],
[ 'NICKEL', 0 ],
[ 'DIME', 0 ],
[ 'QUARTER', 0 ],
[ 'ONE', 0 ],
[ 'FIVE', 15 ],
[ 'TEN', 20 ],
[ 'TWENTY', 60 ],
[ 'ONE HUNDRED', 0 ]
When it should return:
[["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]
So for a strange reason it does well until it reaches below “FIVE”, in that it just stop and doesn´t do “ONE”, “QUARTER”…etc