1
0
Fork 0

Commented code better

This commit is contained in:
Joe L. Wroten 2015-11-01 22:58:47 -06:00
parent 7ebbacf3ac
commit 7c71ff6743

22
app.js
View file

@ -1,37 +1,43 @@
var App = new Vue({ var App = new Vue({
el: '#calculator', el: '#calculator', // Context for 'data' usage
data: { data: {
result: 0, result: 0,
previousResult: NaN, previousResult: NaN,
calculation: '' calculation: '' // String that'll hold our calculation, such as '6+4/2*2'
}, },
methods: { methods: {
// Resets all of our values
clear: function() { clear: function() {
this.result = 0; this.result = 0;
this.previousResult = NaN; this.previousResult = NaN;
this.calculation = ''; this.calculation = '';
}, },
calcAdd: function(thing) { // Takes a character and appends it to our calculation
this.calculation += thing; calcAdd: function(char) {
this.calculation += char;
this.result = this.calculation; this.result = this.calculation;
}, },
// Always removes character at the end of our string
calcRemove: function() { calcRemove: function() {
if (typeof this.calculation === 'string' && this.calculation.length) { if (typeof this.calculation === 'string' && this.calculation.length) {
this.calculation = this.calculation.slice(0, -1); this.calculation = this.calculation.slice(0, -1);
this.result = this.calculation; this.result = this.calculation;
} }
}, },
// Perform the calculation and display the results
equals: function() { equals: function() {
// Strip out any non-numeric character or simple math expressions // Strip out any non-numeric character or simple math expressions
var calcSafe = this.calculation.replace(/[^0-9\+\-\*\/]/g, ''); var calcSafe = this.calculation.replace(/[^0-9\+\-\*\/]/g, '');
// If our calculation starts with an operator, we prepend the previous result to the calculation
if (isNaN(calcSafe.charAt(0))) calcSafe = this.previousResult + calcSafe; if (isNaN(calcSafe.charAt(0))) calcSafe = this.previousResult + calcSafe;
// Do the math, round to nearest whole number (as per project requirements)
try { try {
this.result = Math.round(eval(calcSafe)); // Eval is safe here because of the above regex this.result = Math.round(eval(calcSafe)); // Eval is safe here because of the above regex
this.previousResult = this.result; this.previousResult = this.result; // Set previousResult for next time
this.calculation = ''; this.calculation = ''; // Clean slate our calculation for a new one
} catch (e) { } catch (e) {
console.warn('Invalid Calculation'); console.warn('Invalid Calculation'); // Something went wrong, but most likely the user has an operation at the end of the calculation so don't do anything.
} }
} }
} }
@ -62,7 +68,9 @@ $(document).ready(function() {
App.calcAdd('+'); App.calcAdd('+');
break; break;
default: default:
// Keyboard numbers range from 48 to 57...
if (event.keyCode >= 48 && event.keyCode <= 57) { if (event.keyCode >= 48 && event.keyCode <= 57) {
// ...so leverage this by subtracting 48 from the keyCode and just pass it on to the calculation.
App.calcAdd(event.keyCode - 48); App.calcAdd(event.keyCode - 48);
} }
} }