Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                function checkCashRegister(price, cash, cid) {
  //asigning variables named after each currency to their respective places in their cid.
  let penny = cid[0][1], nickel = cid[1][1], dime = cid[2][1], quarter = cid[3][1], one = cid[4][1], five = cid[5][1], ten = cid[6][1], twenty = cid[7][1], hundred = [8][1];
  let balance = cash - price;
  //currency dictionary, will be useful for later.
  let currency = {
    PENNY: 0.01,
    NICKEL: 0.05,
    DIME: 0.1,
    QUARTER: 0.25,
    DOLLAR: 1,
    FIVE: 5,
    TEN: 10,
    TWENTY: 20,
    HUNDRED: 100
  }

  //gets the total value of the cid;
  let fakemoney = 0;;
  cid.forEach(val => {
    fakemoney += val[1]
  })
  let money =  parseFloat(fakemoney.toFixed(2)) //rounding to nearest hundredth
  
  
  if(money < balance){
    return {status: "INSUFFICIENT_FUNDS", change: []} //if the total money in the cid is lesser than the balance .
  }
  
  else if(money == balance){
    return {status: "CLOSED", change: cid} //if total money is equal to value of cid.
  }
  
  else if(money > balance){
    //checks for perfect value. 
    //checks for quarter, nickel, dime, 1 dollar ,etc respectively
    if(balance == 0.5 && quarter > balance){
      return {status: "OPEN", change: [["QUARTER", balance]]}
    }else if(balance == 0.01 && penny > balance){
      return {status: "OPEN", change: [["PENNY", balance]]}
    }else if(balance == 0.05 && nickel > balance){
      return {status: "OPEN", change: [["NICEKL", balance]]}
    }else if(balance == 0.1 && dime > balance){
      return {status: "OPEN", change: [["DIME", balance]]}
    }else if(balance == 1 && dollar > balance){
      return {status: "OPEN", change: [["DOLLAR", balance]]}
    }else if(balance == 5 && five > balance){
      return {status: "OPEN", change: [["Five Dollars", balance]]}
    }else if(balance == 10 && ten > balance){
      return {status: "OPEN", change: [["Ten Dollars", balance]]}
    }else if(balance == 20 && twenty > balance){
      return {status: "OPEN", change: [["Twenty Dollars", balance]]}
    }else if(balance > 20 && balance < 100){ 
      //this one's different  - it checks for cases when no perfect value is there.
      
      let tempBalance = balance //asigning balance to a new variable called tempBalance
      
      //assigning a variable for each currency and creating an array for each currency respectively. The zero will be incremented accordingly.
      let twarr = ["TWENTY", 0];
      let tarr = ["TEN", 0];
      let farr = ["FIVE", 0];
      let arr = ["ONE", 0];
      let qarr = ["QUARTER", 0];
      let darr = ["DIME", 0];
      let narr = ["NICKEL", 0]
      let parr = ["PENNY", 0]
      
      //dividing to find how many of each currency is there.
      let twavab = twenty / currency.TWENTY
      let tavab = ten / currency.TEN;
      let favab = five / currency.FIVE;
      let avab = one / currency.DOLLAR;
      let qavab = quarter / currency.QUARTER;
      let davab = dime / currency.DIME;
      let pavab = penny / currency.PENNY;
      let navab = nickel / currency.NICKEL
      let finalchange = [];

      do{
        if((tempBalance - currency["TWENTY"]) > 0 && twarr[1] < twavab){
            tempBalance -= currency["TWENTY"]
            tempBalance = parseFloat(tempBalance.toFixed(2));
            twarr[1] ++;
          
        }else if((tempBalance - currency["TEN"]) > 0 && tarr[1] < tavab){
          tempBalance -= currency["TEN"]
          tempBalance = parseFloat(tempBalance.toFixed(2));
          tarr[1] ++;
          
        }else if((tempBalance - currency["FIVE"]) > 0 && farr[1] < favab){
          tempBalance -= currency["FIVE"]
          tempBalance = parseFloat(tempBalance.toFixed(2));
          farr[1] ++;
          
        }else if((tempBalance - currency["DOLLAR"]) > 0 && arr[1] < avab){
          tempBalance -= currency["DOLLAR"]
          tempBalance = parseFloat(tempBalance.toFixed(2));
          arr[1] ++;
          
        }else if((tempBalance - currency["QUARTER"]) > 0 && qarr[1] < qavab){
          tempBalance -= currency["QUARTER"]
          tempBalance = parseFloat(tempBalance.toFixed(2));
          qarr[1] ++;
          
        }else if((tempBalance - currency["DIME"]) > 0 && darr[1] < davab){
          tempBalance -= currency["DIME"]
          tempBalance = parseFloat(tempBalance.toFixed(2));
          darr[1] ++;
          
        }else if((tempBalance - currency["NICKEL"]) > 0 &&narr[1] < navab){
          tempBalance -= currency["NICKEL"]
          tempBalance = parseFloat(tempBalance.toFixed(2));
          narr[1] ++;
          
        }else if((tempBalance - currency["PENNY"]) >= 0 &&  parr[1] < pavab){
          tempBalance -= currency["PENNY"]
          tempBalance = parseFloat(tempBalance.toFixed(2));
          parr[1] ++;
          
        }
      }while(tempBalance >= 0)

      //now it multiplies the number of times the currency by the real value of the currency itself.
      twarr[1]*=20;
      tarr[1]*=10;
      farr[1]*=5;
      arr[1]*=1;
      qarr[1]*=0.25;
      darr[1]*=0.1;
      narr[1]*=0.05;
      parr[1]*=0.01

      let tempchange=[]; //creating a temporary array with all the values.
      tempchange.push(twarr, tarr, farr, arr, qarr, darr, narr, parr)
      
      //now it checks and pushes only the values greater than 0 to the real array
      for(let i=0; i < tempchange.length; i++){
        if(tempchange[i][1] != 0){
          finalchange.push(tempchange[i])
        }
      }
      //finally returns the status and change with the final array;
      return {status: "OPEN", change: finalchange}
    }
    
    //if none of these cases apply, it might mean that there isnt enough to pay back as change even though the money might be lesser than cash in cid. If so , return INSUFFICIENT_FUNDS. Might work for other out-of-the-box cases also.
    else {
    return {status: "INSUFFICIENT_FUNDS", change: []}
    }

  }  
  

}



console.log(checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));
              
            
!
999px

Console