/* AP6 Coding KATA
* StringCalculator with Javascript and TDD through Jasmine.
* Kata link ................. http://osherove.com/tdd-kata-1/
* Jasmine documentation ..... https://jasmine.github.io/
*/
// Our Specs are here
describe("The wonderful StringCalculator project...", function() {
it("- should evaluate 0 for an empty string input", function() {
expect(StringCalculator("")).toBe(0);
});
it("- should evaluate the sum of two numbers given as input separated by a 'space'", function() {
expect(StringCalculator("1 2")).toBe(3);
});
it("- should evaluate the sum of n numbers separated by 'space'", function() {
expect(StringCalculator("1 16 21 4 2")).toBe(44);
});
});
// Our Code is here
function StringCalculator(str) {
result = 0;
if (str.length != 0) {
numbers = str.split(' ');
for (i = 0; i < numbers.length; i++) {
result += Number(numbers[i]);
}
}
return result;
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.