Created
September 15, 2017 13:58
-
-
Save unsign3d/6e51e20bace60e2e1d1ddd4a2a875d34 to your computer and use it in GitHub Desktop.
kata from TDD workshop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FUNCTION | |
const add = (input) => { | |
if (input=="")return 0; | |
input = input.substr(2,input.lenght); | |
var numbers=input.split(/\D+/g).map((num) => parseInt(num)); | |
var sum = 0; | |
for (var i = 0; i < numbers.length; i++) { | |
sum += numbers[i]; | |
} | |
return sum; | |
} | |
test('When it has 0 arguments will return 0', ()=>{ | |
expect(add('')).toBe(0); | |
}); | |
test('When it has 1 argument will return the value of this argument', ()=>{ | |
expect(add('5')).toBe(5); | |
}); | |
test('When it has 2 arguments will return the sum of these arguments', ()=>{ | |
expect(add('5,6')).toBe(11); | |
}); | |
test('When it has seven number of arguments will return the sum of these arguments', ()=>{ | |
expect(add('1,2,3,4,5,6,7')).toBe(28); | |
}); | |
test('When the values are separated with new line it should work', ()=>{ | |
expect(add('1\n2,3')).toBe(6); | |
}); | |
test('When the values are separated with special separator it should work', ()=>{ | |
expect(add("//;\n1;2")).toBe(3); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment