Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!doctype html>
<html>
<head>
<script type = "text/javascript">
function displaynum(n1) {
calcform.display.value=calcform.display.value+n1;
}
</script>
</head>
<body>
<form name="cashreg">
<input type = "text" id="display" style = "text-align:right" value="0"><br>
<input type = "button" id="btn9" value="9">
<input type = "button" id="btn8" value="8">
<input type = "button" id="btn7" value="7">
<input type = "button" id="btn6" value="6">
<input type = "button" id="depbtn" value = "Deposit" ><br>
<input type = "button" id="btn5" value="5">
<input type = "button" id="btn4" value="4">
<input type = "button" id="btn3" value="3">
<input type = "button" id="btn2" value="2">
<input type = "button" id="witbtn" value = "Withdraw" ><br>
<input type = "button" id="btn1" value="1">
<input type = "button" id="btn0" value="0">
<input type = "button" id="addbtn" value = + >
<input type = "button" id="subbtn" value = - >
<input type = "button" id="balbtn" value = "Get Balance" ><br>
<input type = "button" id="equalbtn" value = = >
<input type = "button" id="potbtn" value = . >
<input type = "button" id="btnmul" value = * >
<input type = "button" id="divbtn" value = / >
<input type = "button" id="btnper" value = % >
<input type = "button" id= "clear" value ="clear">




<script src="./js/calculator.js" type="text/javascript"></script>
<script src="./js/cash_register.js" type="text/javascript"></script>
</body>
</html>
123 changes: 123 additions & 0 deletions js/calculator.js
Original file line number Diff line number Diff line change
@@ -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
*/
}
}
99 changes: 99 additions & 0 deletions js/cash_register.js
Original file line number Diff line number Diff line change
@@ -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);