diff --git a/index.html b/index.html index e69de29..c958699 100644 --- a/index.html +++ b/index.html @@ -0,0 +1,41 @@ + + + + + + +
+
+ + + + +
+ + + + +
+ + + + +
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/js/calculator.js b/js/calculator.js index e69de29..c443e1f 100644 --- a/js/calculator.js +++ b/js/calculator.js @@ -0,0 +1,123 @@ +/** + * Declare a function named `calculatorModule` + * this function will have two private variables declared inside of it. + * @variable PRIVATE { Number } `memory` + * @variable PRIVATE { Number } `total` + * @return {object} `calculator` object that can be used + */ +var Calculator = function() { + var memory = 0; + var total = 0; +return { + + + + /** + * sets the `total` to the number passed in + * @param { Number } x + * @return { Number } current total + */ + + +load: (num) => { + if(typeof num === 'number') { + total = num; + return num; + } else { + throw new Error(); + } + }, + + + /** + * Return the value of `total` + * @return { Number } + */ + getTotal: () => { + return total; + }, + + + /** + * Sums the value passed in with `total` + * @param { Number } x + */ + add: (num) => { + if(typeof num === 'number') { + total = total + num; + return total; + } else { + throw new Error(); + } + }, + + /** + * Subtracts the value passed in from `total` + * @param { Number } x + */ + +subtract: (num) => { + if(typeof num === 'number') { + total = total - num; + return total; + } else { + throw new Error(); + } +}, + + /** + * Multiplies the value by `total` + * @param { Number } x + */ +multiply: (num) => { + if(typeof num === 'number') { + total = total * num; + return total; + } else { + throw new Error(); + } +}, + + /** + * Divides the value passing in by `total` + * @param { Number } x + */ + +divide: (num) => { + if(typeof num === 'number') { + total = total / num; + return total; + } else { + throw new Error(); + } +}, + + /** + * Return the value stored at `memory` + * @return { Number } + */ +recallMemory: () => { + return memory; + +}, + + + /** + * Stores the value of `total` to `memory` + */ +saveMemory: () => { + return memory = total; +}, + + /** + * Clear the value stored at `memory` + */ +clearMemory: () => { + return memory = 0; +}, + + /** + * Validation + */ +} +} \ No newline at end of file diff --git a/js/cash_register.js b/js/cash_register.js index e69de29..e59ebfb 100644 --- a/js/cash_register.js +++ b/js/cash_register.js @@ -0,0 +1,99 @@ + +for(var i = 0; i < 10; i++) { + var btn = document.getElementById("btn" + i); + btn.addEventListener("click", function(event) { + display(event.target.value); + }, false); +} + +var displayElement = document.getElementById("display"); + + +var myCalculator = Calculator(); +var clearDisplay = true; + +var bankBalance = 0; + +function display(val) { + if (clearDisplay) { + clear(); + clearDisplay = false; + } + + displayElement.value += val; +} + +function clear() { + displayElement.value = ''; +} + +function add() { + myCalculator.add(Number(displayElement.value)); + clearDisplay = true; + display(myCalculator.getTotal()); + clearDisplay = true; +} + +function sub() { + myCalculator.subtract(Number(displayElement.value)); + clearDisplay = true; + display(myCalculator.getTotal()); + clearDisplay = true; +} + +function multiply() { + myCalculator.multiply(Number(displayElement.value)); + clearDisplay = true; + display(myCalculator.getTotal()); + clearDisplay = true; +} + +function divide() { + myCalculator.divide(Number(displayElement.value)); + clearDisplay = true; + display(myCalculator.getTotal()); + clearDisplay = true; +} + +function equals() { + console.log('test') + myCalculator.getTotal('total'); + clearDisplay = true; + display(myCalculator.getTotal()); + clearDisplay = true; +} + +function clearTotal() { + myCalculator.load(0); + clearDisplay = true; + display(myCalculator.getTotal()); + clearDisplay = true; +} + + +function deposit(amt) { + bankBalance += amt; + getBalance(); +} + +function withdraw(amt) { + console.log("test") + bankBalance -= amt; + getBalance(); +} + +function getBalance() { + clearDisplay = true; + display(bankBalance); + clearDisplay = true; +} + +document.getElementById('addbtn').addEventListener('click', add); +document.getElementById('subbtn').addEventListener('click', sub); +document.getElementById('btnmul').addEventListener('click', multiply); +document.getElementById('divbtn').addEventListener('click', divide); +document.getElementById('equalbtn').addEventListener('click', equals); +document.getElementById('clear').addEventListener('click', clearTotal); +document.getElementById('depbtn').addEventListener('click', deposit); +document.getElementById('balbtn').addEventListener('click', getBalance); +document.getElementById('witbtn').addEventListener('click', withdraw); \ No newline at end of file