AP6 CodingKATA 1

String Calculator: a TDD approach with Jasmine.

by Alex GUYON DE CHEMILLY

HTML

<script src="https://jasmine.github.io/2.2/lib/jasmine.js"></script>
<script src="https://jasmine.github.io/2.2/lib/jasmine-html.js"></script>
<link rel="stylesheet" href="https://jasmine.github.io/2.2/lib/jasmine.css">
<script src="https://jasmine.github.io/2.2/lib/boot.js"></script>

JavaScript

/* 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;
}