HTML and CSS Reference
In-Depth Information
});
</script>
As you can see, there are lots of assertions, but this is to ensure that every
aspect of the application is covered. You can go into more depth within your
unit tests, such as making sure that errors are thrown when invalid values such
as letters are passed to the methods; this, however, won't be covered in this
chapter.
With the unit tests complete, it's now time to write the code for the calculator.
As this section is focused on creating the unit tests, the calculator JavaScript
code won't be explained in great detail. The code comments should help to
explain it a little.
var app = app || {};
app.calculator = function(_initialValue){
/**
* The current result of the calculator
*/
var _result = _initialValue;
/**
* Gets the current result of the calculator
*/
this.getResult = function(){
return _result;
}
/**
* Adds a value to the current result and returns the new value
*/
this.add = function(value){
_result = _result + value;
return _result;
}
/**
* Subtracts a value from the current result and returns the new value
*/
this.subtract = function(value){
_result = _result - value;
return _result;
}
/**
* Multiplies a value from the current result and returns the new value
 
Search WWH ::




Custom Search