Cash Register Problem

function checkCashRegister(price, cash, cid) {
   let currency = [
   ["PENNY" , 0.01],
    ["NICKEL", 0.05],
    ["DIME", 0.1],
    ["QUARTER", 0.25],
    ["ONE" , 1],
    ["FIVE", 5],
    ["10" , 10],
    ["TWENTY" , 20],
    ["ONE HUNDRED", 100]]

  let change = cash - price
  let matchedArr = [] 
  let match;
  let matchCurr = findingCurr()
   let matchedCurr;
  let statObj= {status: "", change:[]}
  
function findingCurr(){
  for (let i = 0; i < currency.length; i++){
   if(currency[i][1] <= change){
     matchedArr.push(currency[i][1])  
   } 
   match = Math.max(...matchedArr) 
  
  }
   for (let i = 0; i < currency.length; i++){
 if (match === currency[i][1]){
    matchedCurr = currency[i][0]
    console.log(matchedCurr)   
    return matchedCurr
 }
   }   
}    
   for (let i = 0; i < cid.length; i++){
     if (matchedCurr === cid[i][0] && cid[i][1] >= change){
       cid[i][1] =  cid[i][1] - match;
       statObj.status = "OPEN"  
       change = change - match
       statObj.change.push([cid[i][0], change])
       findingCurr()  
     }   
     else if (matchCurr === cid[i][0] && cid[i][1] < change){
       statObj.status = "INSUFFICIENT_FUNDS"  
     }
     else if (matchCurr === cid[i][0] 
     && cid[i][1] === change) {
       statObj.status = "CLOSED"
       statObj.change.push([cid[i][0], change])  
     }
     
   }
   console.log(statObj)
      return statObj;
 
}

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

Hi, I am not sure why function call findingCurr() is not working when it is called here-

  if (matchedCurr === cid[i][0] && cid[i][1] >= change){
       cid[i][1] =  cid[i][1] - match;
       statObj.status = "OPEN"  
       change = change - match
       statObj.change.push([cid[i][0], change])
       findingCurr()  
     } ```

Please help :)

First, you need to be clear about what the function findingCurr does? What is the purpose of findCurr in the below code?

  if (matchedCurr === cid[i][0] && cid[i][1] >= change){
       cid[i][1] =  cid[i][1] - match;
       statObj.status = "OPEN"  
       change = change - match
       statObj.change.push([cid[i][0], change])
       findingCurr()  
     }

Ideally, a function should take some parameters and return a value. Your function is using variables defined outside the function and returning undefined or a value. The variables used within the function need to be defined within the function only; for example the variable match.

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