Skip to content

Completed Calcualtor #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
155 changes: 103 additions & 52 deletions calculator.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*jshint esversion: 6 */
/**
* Declare a function named `calculatorModule`
* this function will have two private variables declared inside of it.
Expand All @@ -6,60 +7,110 @@
* @return {object} `calculator` object that can be used
*/

function calculatorModule(){

/**
* sets the `total` to the number passed in
* @param { Number } x
* @return { Number } current total
*/


/**
* Return the value of `total`
* @return { Number }
*/


/**
* Sums the value passed in with `total`
* @param { Number } x
*/


/**
* Subtracts the value passed in from `total`
* @param { Number } x
*/


/**
* Multiplies the value by `total`
* @param { Number } x
*/


/**
* Divides the value passing in by `total`
* @param { Number } x
*/


/**
* Return the value stored at `memory`
* @return { Number }
*/


/**
* Stores the value of `total` to `memory`
*/


/**
* Clear the value stored at `memory`
*/
let memory = 0;
let total = 0;

/**
* Validation
*/

function validate (x) {
if (typeof x !== 'number') {
throw new Error('Not a number!');
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything below the validate function is indented one too far.


/**
* 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;
}

/**
* Return the value of `total`
* @return { Number }
*/
let getTotal = function () {
return total;
}

/**
* Sums the value passed in with `total`
* @param { Number } x
*/
let add = function (x) {
validate(x);
total += x;
return total;
}

/**
* Subtracts the value passed in from `total`
* @param { Number } x
*/
let subtract = function (x) {
validate(x);
total -= x;
return total;
}

/**
* Multiplies the value by `total`
* @param { Number } x
*/
let multiply = function (x) {
validate(x);
total = total * x;
return total;
}

/**
* Divides the value passing in by `total`
* @param { Number } x
*/
let divide = function (x) {
validate(x);
total = total / x;
return total;
}

/**
* Return the value stored at `memory`
* @return { Number }
*/
let recallMemory = function () {
return memory;
}

/**
* Stores the value of `total` to `memory`
*/
let saveMemory = function () {
memory = total;
}

/**
* Clear the value stored at `memory`
*/
let clearMemory = function () {
memory = 0;
}

return {
load: load,
getTotal: getTotal,
add: add,
subtract: subtract,
multiply: multiply,
divide: divide,
recallMemory: recallMemory,
saveMemory: saveMemory,
clearMemory: clearMemory
}
}