JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:

Please help if you can! I have this code working almost, but for some reason it does not give the last penny in change! Can anyone explain why this is? I have been using pythontutor.com and everything goes fine, but when it gets to this block of code:

  let funkyCid = cid.reverse();
  for(let i = 0; i < funkyCid.length; i++){     
  if(change - funkyMoney[i] >= 0 && funkyCid[i][1] - funkyMoney[i] >= 0) {
    while(funkyCid[i][1] - funkyMoney[i] >= 0 && change - funkyMoney[i]>= 0){
      funkyCount[i] += 1;
      change -= funkyMoney[i];
      funkyCid[i][1] -= funkyMoney[i];
    }
  }

it will not stay in the while loop one more time in order to give the last penny in change. Then the rest of it executes and it dishes out the correct change, except for that last penny.

I am sure this code is overcomplicated but it is the way I could figure it out step by step.

Any help or insight would be greatly appreciated! Two weeks working on this code and I am just very stuck.

Here is my huge code so far:

function checkCashRegister(price, cash, cid) {
    let change = cash - price;
    let sum = 0;
    for(let i = 0; i < cid.length; i++) {
       sum += cid[i][1];
    }
    let funkyMoney = [ 100, 20, 10, 5, 1, .25, .10, .05, .01]
    let funkyCount = [0, 0 ,0 ,0 ,0 ,0 ,0 ,0, 0]
    if(change < 0 || sum - change < 0) {
      return {status: "INSUFFICIENT_FUNDS", change: []};
    } else if(change === sum) {
      return {status: 'CLOSED', change: [cid]};
    } else if(change > 0) {
         
      
      let funkyCid = cid.reverse();
   for(let i = 0; i < funkyCid.length; i++){     
      if(change - funkyMoney[i] >= 0 && funkyCid[i][1] - funkyMoney[i] >= 0) {
        while(funkyCid[i][1] - funkyMoney[i] >= 0 && change - funkyMoney[i]>= 0){
          funkyCount[i] += 1;
          change -= funkyMoney[i];
          funkyCid[i][1] -= funkyMoney[i];
        }
}
}
        let changer2 = [];
        function funkie(arr1, arr2) {
          for(let i = 0; i < arr1.length; i++) {
            changer2.push(arr1[i] * arr2[i]) 
          }
        }
      
      funkie(funkyMoney, funkyCount);
      let funkyChangeReturned = [];
      let currencyFunky = [
        ["ONE HUNDRED", 0],
        ["TWENTY", 0],
        ["TEN", 0],
        ["FIVE", 0],
        ["ONE", 0],
        ["QUARTER", 0],
        ["DIME", 0],
        ["NICKEL", 0],
        ["PENNY", 0]
    ];
  for(let i = 0; i < changer2.length; i++) {
currencyFunky[i][1] = changer2[i];
 }
for(let i = 0; i < currencyFunky.length; i++) {
if(currencyFunky[i][1] !== 0) {
funkyChangeReturned.push(currencyFunky[i]);
}
} 

 return { status: "OPEN", change: funkyChangeReturned};
}
  
}
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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

JavaScript Algorithms and Data Structures Projects - Cash Register

Hey, I ran the code in another editor and found no problems.

Hi there. You stuffed your array of arrays inside another array which could be breaking a test. Let me know if that helps at all.

huh… I tried it in VS code as well and it still does just 3 pennies instead of four. Where did you try it? I wonder why it worked for you? Thanks for your help!

Thanks for commenting! Good catch, I am not sure why I did that. I took that out which probably helped, but still didn’t solve the 3 but not 4 pennies. :confused:

try to watch this video:

1 Like

It was on Codepen but that was just for the small part you said was giving you the errors on pythonturtor, but I did run all of you code and it came back with a closing error and cid was undefined. You may want to take a look at this before moving on, this can be written with a series of if statements and returns and no for-loops.

FreeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register.english.md at master · Manish-Giri/FreeCodeCamp · GitHub

1 Like

:exploding_head: okay that makes a lot of sense. I did not know that but it answers so many questions. Thank you for posting this video! So helpful.

Thank you! I will take a look. :slight_smile:

1 Like

I was able to just edit the code so the loop would continue through a negative number that was essentially zero, instead of actually zero, thanks to the floating numbers that are hanging around…and then it continued to grab the last penny. BOOYAH solved it. Thanks again!