Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <div id="top-half">
  <form id="top-half-content">
    <label for="cash-entry">Amount due: $12.95<br><span style="font-size: 12px;"> (In register: $335.41)</span></label>
    $ <input id="cash-entry" type="text" name="cash-entry" placeholder="Ex: 95">
  </form>
</div>

<div id="bottom-half">
  <p id="output">Payment due!</p>
</div>
              
            
!

CSS

              
                /*Variables*/
$primary-default: #152B42;
$primary-light: #1159A6;
$primary-dark: #152B42;

$primary-default-text: white;
$primary-light-text: black;
$primary-dark-text: white;

$font-family: 'Kufam', cursive;

/*Styles*/
body {
  font-family: $font-family;
  margin: 0;
  padding: 0;
}

#top-half {
  //Layout
  position: relative;
  width: 100vw;
  height: 50vh;
  min-height: 200px;
  
  //Text
  line-height: 50vh;
  
  //Design
  color: $primary-default-text;
  background-color: $primary-default;
  
  form {
    //Layout
    position: relative;
    display: inline-block;
    width: 100vw;
    height: 50vh;
    
    //Text
    text-align: center;
    line-height: 2rem;
    
    label {
      //Layout
      position: relative;
      width: 100vw;
      
      //Text
      font-size: 30px;
      font-weight: bold;
    }
    
    input {
      //Layout
      position: relative;
      width: 80%;
      height: 8vh;
      min-height: 40px;
      padding: 10px;
      
      //Text
      text-align: center;
      
      //Design
      border-radius: 20px;
      border: 0;
      outline: none;
      box-shadow: 0 0 5px black;
    }
  }
}

#bottom-half {
  //Layout
  position: relative;
  width: 100vw;
  height: 50vh;
  min-height: 200px;
  border: 1px solid;
  
  //Text
  font-size: 20px;
  line-height: 50vh;
  text-align: center;
  
  //Design
  color: $primary-dark;
  
  p {
    //Layout
    position: relative;
    display: inline-block;
    
    //Text
    line-height: 1.5rem;
  }
}
              
            
!

JS

              
                const cashEntry = document.querySelector("#cash-entry");
const output    = document.querySelector("#output");
const test = document.querySelector("#test");

function checkCashRegister(price, input, cid, output, displayChange) {
  //Clean input to ensure no invalid characters are submitted
  let cash = input.value;
  let cashCheck = /[\d.]/;
  cash = cash
  .split('')
  .filter(num => cashCheck.test(num))
  .join('');

  input.value = cash;

  //Set some initial variables
  let changeNeeded = Math.round((cash - price)*100)/100; //The change necessary to fulfill the transaction
  let outputDisplay;

  if (changeNeeded < 0) { output.innerHTML = "Payment due!"; return; }
  
  let registerCash = Math.round(cid.reduce((prevAmount, currAmount) => {
    return currAmount[1] + prevAmount;
  },0) * 100) / 100; //The summed total of the money in the register
  
  let denominations = {
    "PENNY": 0.01,
    "NICKEL": 0.05,
    "DIME": 0.1,
    "QUARTER": 0.25,
    "ONE": 1,
    "FIVE": 5,
    "TEN": 10,
    "TWENTY": 20,
    "ONE HUNDRED": 100
  }; //An object that holds the value of each denomination, for cleanliness later on
  
  //Check if the change due is more than the cash available
  if (changeNeeded > registerCash) {
    outputDisplay = { status: "INSUFFICIENT_FUNDS", change: [] };
    output.innerHTML = "Register status: " + outputDisplay.status + "; Change due: $" + changeNeeded;
    return;
  }
  
  //Check if the change due is the same as the cash available
  if (changeNeeded === registerCash) {
    outputDisplay = { status: "CLOSED", change: cid };
    output.innerHTML = "Register status: " + outputDisplay.status + "; Change due: $" + changeNeeded + "<br>" + displayChange(outputDisplay.change.reverse(), denominations);
    return;
  }
  
  //If both of the conditions above fail, calculate the change required
  let change = changeNeeded; //A change variable that's safe to deduct from (changeNeeded should not be changed)
  let changeDue = cid
    .concat([]) //Force JS to create a new array to avoid mutation
    .reverse()
    .map(denom => {
      let newAmount = 0; //This variable holds the amount of each denomination to be given as change
      
      //We attempt to deduct the full amount of any given denomination, reducing by the value of the denomination in each attempt until successful
      for (let i = denom[1]; i >= 0; i = Math.round((i - denominations[denom[0]])*100)/100) {
        if (Math.round((change - i)*100)/100 >= 0) {
          change = Math.round((change - i)*100)/100; //Deduct the amount of change we need to account for
          newAmount = Math.round((newAmount + i)*100)/100; //Pass along the amount of this denomination we should return to newAmount
          break;
        }
      }
      if (newAmount > 0 ) { return [denom[0], newAmount]; } else { return [denom[0], 0]; } //Map newAmount onto the new array
    })
    .filter(x => x !== undefined); //Remove all undefined values
  
  //Check to see if exact change is impossible, else, return change
  if (change < 0) { return; }
  if (change > 0) {
    outputDisplay = { status: "INSUFFICIENT_FUNDS", change: [] };
    output.innerHTML = "Register status: " + outputDisplay.status + "; Register lacks appropriate denominations.";
  } else {
    outputDisplay = { status: "OPEN", change: changeDue };
    output.innerHTML = "Register status: " + outputDisplay.status + "; Change due: $" + changeNeeded + "<br>" + displayChange(outputDisplay.change, denominations);
  }
  
  //output.innerHTML = "Status: " + outputDisplay["status"] + "; Change: " + outputDisplay["change"];
}

function displayChange(change, denoms) {
  return "<br>Hundreds: x" + (Math.round((change[0][1] / denoms["ONE HUNDRED"]) * 100) / 100) +
    " | Twenties: x" + (Math.round((change[1][1] / denoms["TWENTY"]) * 100) / 100) +
    " | Tens: x" + (Math.round((change[2][1] / denoms["TEN"]) * 100) / 100) +
    " | Fives: x" + (Math.round((change[3][1] / denoms["FIVE"]) * 100) / 100) +
    " | Ones: x" + (Math.round((change[4][1] / denoms["ONE"]) * 100) / 100) +
    " | Quarters: x" + (Math.round((change[5][1] / denoms["QUARTER"]) * 100) / 100) +
    " | Dimes: x" + (Math.round((change[6][1] / denoms["DIME"]) * 100) / 100) +
    " | Nickels: x" + (Math.round((change[7][1] / denoms["NICKEL"]) * 100) / 100) +
    " | Pennies: x" + (Math.round((change[8][1] / denoms["PENNY"]) * 100) / 100);
}


cashEntry.addEventListener("input", () => {
  checkCashRegister(12.95, cashEntry, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]], output, displayChange);
});
              
            
!
999px

Console