Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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