JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
Not able to update temp variable. you can read the section of ( change < totalcid ). I need help only on that section.

  **Your code so far**
function checkCashRegister(price, cash, cid) {

//Change to give to customer
var change = cash - price;
change = change.toFixed(2);

//total cash in drawer
var totalcid = 0;
for (let i = 0; i < cid.length; i++) 
{
    
     totalcid += cid[i][1];
      
}
totalcid = totalcid.toFixed(2);


if (change > totalcid)
{  
    return {status: "INSUFFICIENT_FUNDS", change: []};
}

else if (change < totalcid)
{ 
  //reverse cid parameter
  cid = cid.reverse();
  let changeArr = [];
  let temp;

  
  for (let j = 0; j < cid.length; j++)
  {    
       //If change is greater than cid & also that cid must be greater than zero than substract change and update temp variable 
       if (change >= cid[j][1] && cid[j][1] > 0)
       {
           change -= cid[j][1];
           temp[j][0] += cid[j][0];
           temp[j][1] += change.toFixed(2);
           
       }
       console.log(temp);
       /* My problem is temp variable is not getting update */

  if (temp[j][1] > 0)
  {
     changeArr.push(temp);
     console.log(changeArr);
  }
       
      
  }
 return {status: "Open", change: changeArr};
}      
else if (totalcid === cid )
{
   return {status: "Closed", change: cid}
}


}

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]]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

temp is not an array, so you can’t treat it as such

i have also used temp as an array but its not wroking . also used push method

It seems the only thing you use is temp[j][1], you could have a temp varuable only for this maybe

instead of array of array or something, just a variable holding a number

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