Sum of Numbers | codewars.com

by Sebastian Kay

HAML

%link{rel: "stylesheet", href: "https://bootswatch.com/5/darkly/bootstrap.min.css"}

%div.container

%h4.text-success Sum of Numbers | codewars.com

%p Given two integers a and b, which can be positive or negative, find the sum of all the integers between and including them and return it. If the two numbers are equal return a or b.
%p Note: a and b are not ordered!

%ul
	%li (a, b) --> output (explanation)
	%li (1, 0) --> 1 (1 + 0 = 1)
	%li (1, 2) --> 3 (1 + 2 = 3)
	%li (0, 1) --> 1 (0 + 1 = 1)
	%li (1, 1) --> 1 (1 since both are same)
	%li (-1, 0) --> -1 (-1 + 0 = -1)
	%li (-1, 2) --> 2 (-1 + 0 + 1 + 2 = 2)
	
	%div

CSS

body {
	padding: 10px 50px 10px 50px;
	font-size: 0.9rem;
	margin-right: 50px;
}

div {
	width: 80vw;
	margin-left: 50px;
	margin-right: 50px;
	background:#ff00ff;

}

h4{}

ul{
	list-style: none;
}

JavaScript

function getSum(a,b) {
		return (a==b)?a:(a<b)?((b-a)+1)*(a+b)/2:((a-b)+1)*(b+a)/2;
}

console.log(getSum(0,-1));