From 82b6a35a237d1f2f5cd153c405eb37f60740210d Mon Sep 17 00:00:00 2001 From: John Atkins Date: Wed, 14 Jun 2017 17:26:00 -1000 Subject: [PATCH 1/4] Completed Calcualtor --- calculator.js | 59 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/calculator.js b/calculator.js index 3e5744c..5935229 100644 --- a/calculator.js +++ b/calculator.js @@ -1,3 +1,4 @@ +/*jshint esversion: 6 */ /** * Declare a function named `calculatorModule` * this function will have two private variables declared inside of it. @@ -6,60 +7,100 @@ * @return {object} `calculator` object that can be used */ +function calculatorModule(){ + + let memory = 0; + let total = 0; + + return { /** * sets the `total` to the number passed in * @param { Number } x * @return { Number } current total */ - + load: function (x) { + this.validate(x); + total = x; + return total; + }, /** * Return the value of `total` * @return { Number } */ - + getTotal: function () { + return total; + }, /** * Sums the value passed in with `total` * @param { Number } x */ - + add: function (x) { + this.validate(x); + total += x; + return total; + }, /** * Subtracts the value passed in from `total` * @param { Number } x */ - + subtract: function (x) { + this.validate(x); + total -= x; + return total; + }, /** * Multiplies the value by `total` * @param { Number } x */ - + multiply: function (x) { + this.validate(x); + total = total * x; + return total; + }, /** * Divides the value passing in by `total` * @param { Number } x */ - + divide: function (x) { + this.validate(x); + total = total / x; + return total; + }, /** * Return the value stored at `memory` * @return { Number } */ - + recallMemory: function (x) { + return memory; + }, /** * Stores the value of `total` to `memory` */ - + saveMemory: function (x) { + memory = total; + }, /** * Clear the value stored at `memory` */ - + clearMemory: function (x) { + memory = 0; + }, /** * Validation */ + validate: function (x) { + if (typeof x !== 'number') {return ; } + }, + + }; +} \ No newline at end of file From 80c4743583e2dc421aa2e360c3e706c0be0e4a18 Mon Sep 17 00:00:00 2001 From: John Atkins Date: Wed, 14 Jun 2017 17:33:01 -1000 Subject: [PATCH 2/4] Revised with new error message --- calculator.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/calculator.js b/calculator.js index 5935229..c78be80 100644 --- a/calculator.js +++ b/calculator.js @@ -98,7 +98,11 @@ function calculatorModule(){ * Validation */ validate: function (x) { - if (typeof x !== 'number') {return ; } + if (typeof x !== 'number') { + var err = new Error('Not a number!'); + console.log(err); + return err; + } }, }; From ef4820fe52a193d13b67c061d9799006e700195e Mon Sep 17 00:00:00 2001 From: John Atkins Date: Wed, 14 Jun 2017 19:05:54 -1000 Subject: [PATCH 3/4] Removed params from the ones that don't need them --- calculator.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/calculator.js b/calculator.js index c78be80..6b70cfc 100644 --- a/calculator.js +++ b/calculator.js @@ -77,21 +77,21 @@ function calculatorModule(){ * Return the value stored at `memory` * @return { Number } */ - recallMemory: function (x) { + recallMemory: function () { return memory; }, /** * Stores the value of `total` to `memory` */ - saveMemory: function (x) { + saveMemory: function () { memory = total; }, /** * Clear the value stored at `memory` */ - clearMemory: function (x) { + clearMemory: function () { memory = 0; }, /** @@ -107,4 +107,4 @@ function calculatorModule(){ }; -} \ No newline at end of file +}; \ No newline at end of file From 1f20bc7dc5e77ff4f98c4305c2d031237bf90cd9 Mon Sep 17 00:00:00 2001 From: John Atkins Date: Thu, 15 Jun 2017 09:56:49 -1000 Subject: [PATCH 4/4] Refactor with new pattern --- calculator.js | 174 ++++++++++++++++++++++++++------------------------ 1 file changed, 90 insertions(+), 84 deletions(-) diff --git a/calculator.js b/calculator.js index 6b70cfc..bca2b0d 100644 --- a/calculator.js +++ b/calculator.js @@ -12,99 +12,105 @@ function calculatorModule(){ let memory = 0; let total = 0; - return { - /** - * sets the `total` to the number passed in - * @param { Number } x - * @return { Number } current total + * Validation */ - load: function (x) { - this.validate(x); - total = x; - return total; - }, + function validate (x) { + if (typeof x !== 'number') { + throw new Error('Not a number!'); + } + } - /** - * Return the value of `total` - * @return { Number } - */ - getTotal: function () { - return total; - }, + /** + * sets the `total` to the number passed in + * @param { Number } x + * @return { Number } current total + */ + let load = function (x) { + validate(x); + total = x; + return total; + } - /** - * Sums the value passed in with `total` - * @param { Number } x - */ - add: function (x) { - this.validate(x); - total += x; - return total; - }, + /** + * Return the value of `total` + * @return { Number } + */ + let getTotal = function () { + return total; + } - /** - * Subtracts the value passed in from `total` - * @param { Number } x - */ - subtract: function (x) { - this.validate(x); - total -= x; - return total; - }, + /** + * Sums the value passed in with `total` + * @param { Number } x + */ + let add = function (x) { + validate(x); + total += x; + return total; + } - /** - * Multiplies the value by `total` - * @param { Number } x - */ - multiply: function (x) { - this.validate(x); - total = total * x; - return total; - }, + /** + * Subtracts the value passed in from `total` + * @param { Number } x + */ + let subtract = function (x) { + validate(x); + total -= x; + return total; + } - /** - * Divides the value passing in by `total` - * @param { Number } x - */ - divide: function (x) { - this.validate(x); - total = total / x; - return total; - }, + /** + * Multiplies the value by `total` + * @param { Number } x + */ + let multiply = function (x) { + validate(x); + total = total * x; + return total; + } - /** - * Return the value stored at `memory` - * @return { Number } - */ - recallMemory: function () { - return memory; - }, + /** + * Divides the value passing in by `total` + * @param { Number } x + */ + let divide = function (x) { + validate(x); + total = total / x; + return total; + } - /** - * Stores the value of `total` to `memory` - */ - saveMemory: function () { - memory = total; - }, + /** + * Return the value stored at `memory` + * @return { Number } + */ + let recallMemory = function () { + return memory; + } - /** - * Clear the value stored at `memory` - */ - clearMemory: function () { - memory = 0; - }, - /** - * Validation - */ - validate: function (x) { - if (typeof x !== 'number') { - var err = new Error('Not a number!'); - console.log(err); - return err; - } - }, + /** + * Stores the value of `total` to `memory` + */ + let saveMemory = function () { + memory = total; + } - }; + /** + * Clear the value stored at `memory` + */ + let clearMemory = function () { + memory = 0; + } -}; \ No newline at end of file + return { + load: load, + getTotal: getTotal, + add: add, + subtract: subtract, + multiply: multiply, + divide: divide, + recallMemory: recallMemory, + saveMemory: saveMemory, + clearMemory: clearMemory + } +}