Skip to content
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

Finished #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
58 changes: 56 additions & 2 deletions calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,114 @@
* @return {object} `calculator` object that can be used
*/

function calculatorModule(memory, total) {
let _total = 0;
let _memory = 0;

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

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

function load(x) {

validate(x);
_total = x;
return _total;
}

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

function getTotal() {
return _total;
}

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


function add(x) {
validate(x);
_total += x;
}
/**
* Subtracts the value passed in from `total`
* @param { Number } x
*/

function subtract(x) {
validate(x);
_total -= x;
}

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

function multiply(x) {
validate(x);
_total *= x;
}

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

function divide(x) {
validate(x);
_total /= x;
}

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

function recallMemory() {
return _memory;
}

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

function saveMemory() {
_memory = _total;
}

/**
* Clear the value stored at `memory`
*/

function clearMemory() {
_memory = 0;
}

/**
* Validation
*/

function validate(x) {
if (typeof x !== 'number') {
throw new Error();
}
}
}
162 changes: 162 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.