Skip to content

Instantly share code, notes, and snippets.

@Cspeisman
Last active August 29, 2015 13:57
Show Gist options
  • Save Cspeisman/9857861 to your computer and use it in GitHub Desktop.
Save Cspeisman/9857861 to your computer and use it in GitHub Desktop.
// declares global variables
var newEntry = document.getElementById('newEntry'),total = 0,
printedTotal = document.getElementById('total'),
entryBody = document.getElementById('entries');
// listen for event on the 'entry' form
// calls function executeCashRegister
document.getElementById('entry').addEventListener('submit', executeCashRegister)
// main cash register functionality
function executeCashRegister(event){
// keeps form from submitting
event.preventDefault();
// parse out string into a float
var price = parseFloat(newEntry.value);
listOutPrice('banana',price);
// calculate the total value
total += price;
// set the the total value in the html
printedTotal.innerHTML = formatCurrency(total);
newEntry.value = '';
}
function listOutPrice(item, price){
entryBody.innerHTML += '<tr><td>' + item + '</td><td>'+formatCurrency(price) + '</td></tr>'
}
// takes a number and converts a currency amount
function formatCurrency(num){
return '$'+ num.toFixed(2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment