8000 Complete exercises 4 and 5 · josh-Fen/javascript-exercises@b0c79e8 · GitHub
[go: up one dir, main page]

Skip to content

Commit b0c79e8

Browse files
committed
Complete exercises 4 and 5
1 parent 4d31154 commit b0c79e8

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

04_removeFromArray/removeFromArray.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
const removeFromArray = function() {
1+
const removeFromArray = function (arr, ...args) {
2+
arr = arr.filter((item) => !args.includes(item));
23

4+
return arr;
35
};
46

57
// Do not edit below this line

04_removeFromArray/removeFromArray.spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@ describe('removeFromArray', () => {
44
test('removes a single value', () => {
55
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
66
});
7-
test.skip('removes multiple values', () => {
7+
test('removes multiple values', () => {
88
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
99
});
10-
test.skip('removes multiple of the same value', () => {
10+
test('removes multiple of the same value', () => {
1111
expect(removeFromArray([1, 2, 2, 3], 2)).toEqual([1, 3]);
1212
});
13-
test.skip('ignores non present values', () => {
13+
test('ignores non present values', () => {
1414
expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]);
1515
});
16-
test.skip('ignores non present values, but still works', () => {
16+
test('ignores non present values, but still works', () => {
1717
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
1818
});
19-
test.skip('can remove all values', () => {
19+
test('can remove all values', () => {
2020
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
2121
});
22-
test.skip('works with strings', () => {
22+
test('works with strings', () => {
2323
expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
2424
});
25-
test.skip('only removes same type', () => {
25+
test('only removes same type', () => {
2626
expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]);
2727
});
2828
});

05_sumAll/sumAll.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
const sumAll = function() {
1+
const sumAll = function (first, second) {
2+
if (typeof (first) === 'number' &&
3+
typeof (second) === 'number' &&
4+
first > 0 &&
5+
second > 0) {
6+
const start = first < second ? first : second;
7+
const end = second > first ? second : first;
8+
let answer = 0;
9+
for (let i = start; i <= end; i++) {
10+
answer += i;
11+
}
12+
return answer;
13+
} else {
14+
return "ERROR";
15+
}
216

317
};
418

05_sumAll/sumAll.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ describe('sumAll', () => {
44
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', () => {
13+
test('returns ERROR with negative numbers', () => {
1414
expect(sumAll(-10, 4)).toEqual('ERROR');
1515
});
16-
test.skip('returns ERROR with non-number parameters', () => {
16+
test('returns ERROR with non-number parameters', () => {
1717
expect(sumAll(10, "90")).toEqual('ERROR');
1818
});
19-
test.skip('returns ERROR with non-number parameters', () => {
19+
test('returns ERROR with non-number parameters', () => {
2020
expect(sumAll(10, [90, 1])).toEqual('ERROR');
2121
});
2222
});

0 commit comments

Comments
 (0)
0