JSFiddle - React, Tailwind, and code Playground

by levrun

HTML

<script src="https://cdn.jsdelivr.net/jasmine/2.5.2/jasmine.js"></script>
<script src="https://cdn.jsdelivr.net/jasmine/2.5.2/jasmine-html.js"></script>
<script src="https://cdn.jsdelivr.net/jasmine/2.5.2/boot.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/jasmine/2.5.2/jasmine.css">

TypeScript

class StringCalculator {

	public COMMA_SEPARATOR:string = ",";
  public SET_NEW_DELIMETER_MARKER:string = "//";
  public EMPTY_STRING:string = "";
  public NEW_LINE:string = "\n";

	add(input:string):int {
  
  	if(input.length === 0) {
    	return 0;
    }
    
    let parsedString:string = this.EMPTY_STRING;
    let separator:string = this.COMMA_SEPARATOR;
    
    if(input.substring(0, 2) === this.SET_NEW_DELIMETER_MARKER) {
    	separator = input.substring(2,3);
      input = input.substring(4, input.length);
    }
    
    parsedString = input.replace(this.NEW_LINE, separator);
    
    let array[] = parsedString.split(separator);
    let errors[] = [];
    
    let summ:int = 0;
    for(let i of array) {
    	let num:int = parseInt(i);
      if(num < 0) {
      	errors.push(i);
      }
    	
      summ += num;
    }
    
    if(errors.length > 0) {
    	let errorsNumbers:string = "";
      
      for(let negative:string in errors) {
      	errorsNumbers += errors[negative] + " ";
      }
      
      throw new Error("negatives not allowed " + errorsNumbers);
    }
  
  	return summ;
  }

}

describe("String calculator suite", function() {

	it("Test that empty string return zero", function(done) {
  	StringCalculator calculator = new StringCalculator();
    input = "";
    result = 0;
    expect(calculator.add(input)).toBe(result);
    setTimeout(done, 1000);
  });
  
  it("Test that string with one number return the same number", function(done) {
  	StringCalculator calculator = new StringCalculator();
    input = "1";
    result = 1;
    expect(calculator.add(input)).toBe(result);
    setTimeout(done, 1000);
  });
  
  it("Test that string with two numbers return their summ", function(done) {
  	StringCalculator calculator = new StringCalculator();
    input = "1,3";
    result = 4;
    expect(calculator.add(input)).toBe(result);
    setTimeout(done, 1000);
  });
  
  it("Test that string with many numbers return their summ", function(done) {
 ...