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
126 changes: 69 additions & 57 deletions calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,72 @@
* @return {object} `calculator` object that can be used
*/


/**
* 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`
*/

/**
* Validation
*/

function calculatorModule(){
let _memory = 0;
let _total = 0;

function load (num) {
validate (num);
_total = num;
return _total;
};

function getTotal () {
return _total;
};

function add (num) {
validate (num);
_total += num;
};

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

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

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

function recallMemory () {
return _memory;
};

function saveMemory () {
_memory = _total;
};

function clearMemory () {
_memory = 0;
};

function validate (num) {
if (typeof num !== 'number') {
throw new Error('Try again!');
} else {
return true
}
};

let calculator = {
load: load,
getTotal: getTotal,
add: add,
subtract: subtract,
multiply: multiply,
divide: divide,
recallMemory: recallMemory,
saveMemory: saveMemory,
clearMemory: clearMemory
};

return calculator;

};
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@

</div>
<script src="/calculator.js" type="text/javascript"></script>
<script src=js/calculator.js> </script>
<script src=js/cash-register.js></script>
</body>
</html>
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.