Last active
August 29, 2015 13:57
-
-
Save Cspeisman/9857861 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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