8000 odin project completed exercises · sabs/javascript-exercises@6b88882 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6b88882

Browse files
committed
odin project completed exercises
1 parent 3e530e3 commit 6b88882

File tree

15 files changed

+3860
-2218
lines changed

15 files changed

+3860
-2218
lines changed

01_helloWorld/helloWorld.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const helloWorld = function() {
2-
return ''
2+
return 'Hello, World!'
33
};
44

55
module.exports = helloWorld;

02_repeatString/repeatString.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
const repeatString = function() {
2-
1+
const repeatString = function (str='', num=0) {
2+
let repeatedStr = '';
3+
if (num < 0) {
4+
return 'ERROR';
5+
}
6+
for (let x = 0; x < num; x++) {
7+
repeatedStr += str;
8+
}
9+
return repeatedStr;
310
};
411

512
// Do not edit below this line

02_repeatString/repeatString.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ describe('repeatString', () => {
44
test('repeats the string', () => {
55
expect(repeatString('hey', 3)).toEqual('heyheyhey');
66
});
7-
test.skip('repeats the string many times', () => {
7+
test('repeats the string many times', () => {
88
expect(repeatString('hey', 10)).toEqual('heyheyheyheyheyheyheyheyheyhey');
99
});
10-
test.skip('repeats the string 1 times', () => {
10+
test('repeats the string 1 times', () => {
1111
expect(repeatString('hey', 1)).toEqual('hey');
1212
});
13-
test.skip('repeats the string 0 times', () => {
13+
test('repeats the string 0 times', () => {
1414
expect(repeatString('hey', 0)).toEqual('');
1515
});
16-
test.skip('returns ERROR with negative numbers', () => {
16+
test('returns ERROR with negative numbers', () => {
1717
expect(repeatString('hey', -1)).toEqual('ERROR');
1818
});
19-
test.skip('repeats the string a random amount of times', function () {
19+
test('repeats the string a random amount of times', function () {
2020
/*The number is generated by using Math.random to get a value from between
2121
0 to 1, when this is multiplied by 1000 and rounded down with Math.floor it
2222
equals a number between 0 to 999 (this number will change everytime you run
@@ -31,7 +31,7 @@ describe('repeatString', () => {
3131
was randomly generated. */
3232
expect(repeatString('hey', number).match(/((hey))/g).length).toEqual(number);
3333
});
34-
test.skip('works with blank strings', () => {
34+
test('works with blank strings', () => {
3535
expect(repeatString('', 10)).toEqual('');
3636
});
3737
});

03_reverseString/reverseString.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const reverseString = function() {
2-
1+
const reverseString = function(str='') {
2+
return str.split('').reverse().join('');
33
};
44

55
// Do not edit below this line
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
const reverseString = require('./reverseString')
1+
const reverseString = require('./reverseString');
22

33
describe('reverseString', () => {
44
test('reverses single word', () => {
55
expect(reverseString('hello')).toEqual('olleh');
66
});
77

8-
test.skip('reverses multiple words', () => {
8+
test('reverses multiple words', () => {
99
expect(reverseString('hello there')).toEqual('ereht olleh')
1010
})
1111

12-
test.skip('works with numbers and punctuation', () => {
12+
test('works with numbers and punctuation', () => {
1313
expect(reverseString('123! abc!')).toEqual('!cba !321')
1414
})
15-
test.skip('works with blank strings', () => {
15+
test('works with blank strings', () => {
1616
expect(reverseString('')).toEqual('')
1717
})
1818
});

04_removeFromArray/removeFromArray.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
const removeFromArray = function() {
1+
const removeFromArray = function (arr, item, ...args) {
2+
let items = Array.from(args);
3+
items.unshift(item);
24

5+
items.forEach(function (e) {
6+
let index = arr.indexOf(e);
7+
if (index >= 0) {
8+
arr.splice(index, 1);
9+
}
10+
});
11+
return arr;
312
};
413

514
// Do not edit below this line

04_removeFromArray/removeFromArray.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ 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('ignores non present values', () => {
10+
test('ignores non present values', () => {
1111
expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]);
1212
});
13-
test.skip('ignores non present values, but still works', () => {
13+
test('ignores non present values, but still works', () => {
1414
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
1515
});
16-
test.skip('can remove all values', () => {
16+
test('can remove all values', () => {
1717
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
1818
});
19-
test.skip('works with strings', () => {
19+
test('works with strings', () => {
2020
expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
2121
});
22-
test.skip('only removes same type', () => {
22+
test('only removes same type', () => {
2323
expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]);
2424
});
2525
});

05_sumAll/sumAll.js

Lines changed: 15 additions & 2 deletions
Original file line numb 10000 erDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
const sumAll = function() {
2-
1+
const sumAll = function (startNum, endNum) {
2+
if (startNum > endNum) {
3+
[startNum, endNum] = [endNum, startNum];
4+
}
5+
if (typeof(startNum) != 'number' || typeof(endNum) != 'number') {
6+
return 'ERROR';
7+
} else if (startNum < 0 || endNum < 0) {
8+
return 'ERROR';
9+
} else {
10+
let sum = 0;
11+
for (let x = startNum; x <= endNum; x++) {
12+
sum += x;
13+
}
14+
return sum;
15+
}
316
};
417

518
// Do not edit below this line

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
});

06_leapYears/leapYears.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
const leapYears = function() {
2-
1+
const leapYears = function(year) {
2+
if (year % 4 == 0) {
3+
if (year % 100 == 0) {
4+
if (year % 400 == 0) {
5+
return true;
6+
} else {
7+
return false;
8+
}
9+
} else {
10+
return true;
11+
}
12+
} else {
13+
return false;
14+
}
315
};
416

517
// Do not edit below this line

0 commit comments

Comments
 (0)
0