Skip to content

Instantly share code, notes, and snippets.

@adomokos
Created October 3, 2010 03:05
Show Gist options
  • Save adomokos/608223 to your computer and use it in GitHub Desktop.
Save adomokos/608223 to your computer and use it in GitHub Desktop.
// TotalCalculator.js
function TotalCalculator() {
}
TotalCalculator.prototype.calculate = function(quantity, price) {
var _quantity = parseInt(quantity);
if (isNaN(_quantity))
return 'N/A';
var _price = parseInt(price.replace('$', ''));
var total = _quantity * _price;
return '$' + total.toFixed(2);
}
// TotalCalculatorSpec.js
describe("TotalCalculator", function() {
var totalCalculator;
beforeEach(function() {
totalCalculator = new TotalCalculator();
});
it('calculates total from integer strings', function() {
var result = totalCalculator.calculate('2', '$15.00');
expect(result).toEqual('$30.00');
});
it('calculates total from double string', function() {
var result = totalCalculator.calculate('2.3', '$15.00');
expect(result).toEqual('$30.00');
});
it('handles non-numeric quantity', function() {
var result = totalCalculator.calculate('He', '$15.00');
expect(result).toEqual('N/A');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment