8000 Complete lesson #4 · bmilcs/odin-javascript-exercises@f96c7d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit f96c7d1

Browse files
committed
Complete lesson TheOdinProject#4
1 parent 601cf54 commit f96c7d1

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

05_sumAll/sumAll.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
1-
const sumAll = function() {
1+
const sumAll = function (num1, num2) {
2+
// declare var to hold final sum
3+
let finalSum = 0;
24

5+
// if num 1 > num2, swap values
6+
if (num1 > num2) {
7+
const tempNum = num1;
8+
num1 = num2;
9+
num2 = tempNum;
10+
}
11+
12+
// if either number is negative or parameters contain non-numbers, return ERROR
13+
if (
14+
num1 < 0 ||
15+
num2 < 0 ||
16+
typeof num1 !== "number" ||
17+
typeof num2 !== "number"
18+
)
19+
return "ERROR";
20+
21+
// loop from num1 to num2
22+
for (let i = num1; i <= num2; i++) {
23+
// add each integer to final value
24+
finalSum += i;
25+
}
26+
// return final value
27+
return finalSum;
328
};
429

30+
console.log(sumAll(1, 4));
31+
532
// Do not edit below this line
633
module.exports = sumAll;

05_sumAll/sumAll.spec.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
const sumAll = require('./sumAll')
1+
const sumAll = require("./sumAll");
22

3-
describe('sumAll', () => {
4-
test('sums numbers within the range', () => {
3+
describe("sumAll", () => {
4+
test("sums numbers within the range", () => {
55
expect(sumAll(1, 4)).toEqual(10);
66
});
7-
test.skip('works with large numbers', () => {
7+
test("works with large numbers", () => {
88
expect(sumAll(1, 4000)).toEqual(8002000);
99
});
10-
test.skip('works with larger number first', () => {
10+
test("works with larger number first", () => {
1111
expect(sumAll(123, 1)).toEqual(7626);
1212
});
13-
test.skip('returns ERROR with negative numbers', () => {
14-
expect(sumAll(-10, 4)).toEqual('ERROR');
13+
test("returns ERROR with negative numbers", () => {
14+
expect(sumAll(-10, 4)).toEqual("ERROR");
1515
});
16-
test.skip('returns ERROR with non-number parameters', () => {
17-
expect(sumAll(10, "90")).toEqual('ERROR');
16+
test("returns ERROR with non-number parameters", () => {
17+
expect(sumAll(10, "90")).toEqual("ERROR");
1818
});
19-
test.skip('returns ERROR with non-number parameters', () => {
20-
expect(sumAll(10, [90, 1])).toEqual('ERROR');
19+
test("returns ERROR with non-number parameters", () => {
20+
expect(sumAll(10, [90, 1])).toEqual("ERROR");
2121
});
2222
});

0 commit comments

Comments
 (0)
0