Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions w03/w3-r-sum-of-numbers/problem.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* Given two integers, which can be positive and negative,
/* Given two integers, which can be positive and negative,
find the sum of all the numbers between and including a and b,
and return the sum. If both numbers are equal return a or b.

Note! a and b are not ordered!

Example:
Example:
getSum(1, 0) == 1 // 1 + 0 = 1
getSum(1, 2) == 3 // 1 + 2 = 3
getSum(0, 1) == 1 // 0 + 1 = 1
Expand All @@ -14,13 +14,24 @@ getSum(-1, 2) == 2 // -1 + 0 + 1 + 2 = 2
*/

function getSum( a, b ) {
if(a === b){
return a;
}
if(b < a){
let temp = b;
b = a;
a = temp;
}
var answer = 0;
for (i = a; i <= b; i++) {
answer += i;
}
return answer;

} // END FUNCTION


module.exports = {
getSum:getSum,
attendance:"WORD UP"
}


};
26 changes: 13 additions & 13 deletions w03/w3-r-sum-of-numbers/test/r-test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
var expect = require("chai").expect;

var getSum = require("../solution").getSum;
console.log(getSum.toString(), "TYLRERERKJERHKEJRHKJ")
var getSum = require("../problem").getSum;
console.log(getSum.toString(), "TYLRERERKJERHKEJRHKJ");
describe("getSum warmup", function() {
it("should return the same number if a and b are equal", function () {
expect(getSum(1, 1)).to.be.equal(1)
expect(getSum(7, 7)).to.be.equal(7)
expect(getSum(2928, 2928)).to.be.equal(2928)
})
expect(getSum(1, 1)).to.be.equal(1);
expect(getSum(7, 7)).to.be.equal(7);
expect(getSum(2928, 2928)).to.be.equal(2928);
});

it("should return the addition of each number between and including a & b", function () {
expect(getSum(-1, 2)).to.be.equal(2)
expect(getSum(-10, 2)).to.be.equal(-52)
expect(getSum(77, -77)).to.be.equal(0)
expect(getSum(5, -7)).to.be.equal(-13)
expect(getSum(34554, 2)).to.be.equal(597006734)
})
})
expect(getSum(-1, 2)).to.be.equal(2);
expect(getSum(-10, 2)).to.be.equal(-52);
expect(getSum(77, -77)).to.be.equal(0);
expect(getSum(5, -7)).to.be.equal(-13);
expect(getSum(34554, 2)).to.be.equal(597006734);
});
});