JavaScript Algorithms and Data Structures Projects - Cash Register

Hello, I just want to know how can I iterate in two objects at the same time. It is just that I thought that maybe I can solve this problem by iterating two arrays (the one given at cid and one with the real denominations, meaning {‘ONE HUNDRED’: 100, …}) at the same time. Thank you.

function checkCashRegister(price, cash, cid) {
  let change = cash - price;
  let moneyChange = 0; 

  // copy 'cid', reverse it and transform it into an object
  const reverseCid = [...cid].reverse();
  const objCid = Object.fromEntries(reverseCid);

  // real money in cash register with only two decimals
  let sumCid = 0;
  for (var i in objCid) {
    sumCid += objCid[i];
  }
  let inRegister = Number.parseFloat(sumCid).toFixed(2);

  return inRegister == change ? 
         {status: "CLOSED", change: cid} :
         inRegister < change ?
         {status: "INSUFFICIENT_FUNDS", change: []} :
         inRegister > change ? 
         {status: "OPEN", change: 'let me think about it'} :
         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]]));   
 
/* 
console.log(cid);
console.log(objCid);
console.log(inRegister);

const objCid = Object.fromEntries(cid);
https://www.javascripttutorial.net/object/convert-an-object-to-an-array-in-javascript/
*/

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

You asked about two arrays, but your other example is an object, and your code turns cid from an array to an object? If its for two arrays, notice how they always give you every denomination every function call in the cid? Maybe you could use a similar index with both. If its with an object you could use a key to get a value from an object.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.