Skip to content

Commit 8dfb7e4

Browse files
Merge pull request TheOdinProject#234 from thatblindgeye/update_solutions
Exercises: Add solution directories for all exercises
2 parents 8746ce0 + e416717 commit 8dfb7e4

37 files changed

+734
-9
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const helloWorld = function () {
2+
return "Hello, World!";
3+
};
4+
5+
module.exports = helloWorld;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const helloWorld = require('./helloWorld-solution');
2+
3+
describe('Hello World', function () {
4+
test('says "Hello, World!"', function () {
5+
expect(helloWorld()).toEqual('Hello, World!');
6+
});
7+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const repeatString = function (word, times) {
2+
if (times < 0) return "ERROR";
3+
let string = "";
4+
for (let i = 0; i < times; i++) {
5+
string += word;
6+
}
7+
return string;
8+
};
9+
10+
module.exports = repeatString;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const repeatString = require('./repeatString-solution');
2+
3+
describe('repeatString', () => {
4+
test('repeats the string', () => {
5+
expect(repeatString('hey', 3)).toEqual('heyheyhey');
6+
});
7+
test('repeats the string many times', () => {
8+
expect(repeatString('hey', 10)).toEqual('heyheyheyheyheyheyheyheyheyhey');
9+
});
10+
test('repeats the string 1 times', () => {
11+
expect(repeatString('hey', 1)).toEqual('hey');
12+
});
13+
test('repeats the string 0 times', () => {
14+
expect(repeatString('hey', 0)).toEqual('');
15+
});
16+
test('returns ERROR with negative numbers', () => {
17+
expect(repeatString('hey', -1)).toEqual('ERROR');
18+
});
19+
test('repeats the string a random amount of times', function () {
20+
/*The number is generated by using Math.random to get a value from between
21+
0 to 1, when this is multiplied by 1000 and rounded down with Math.floor it
22+
equals a number between 0 to 999 (this number will change everytime you run
23+
the test).*/
24+
25+
// DO NOT use Math.floor(Math.random() * 1000) in your code,
26+
// this test generates a random number, then passes it into your code with a function parameter.
27+
// If this doesn't make sense, you should go read about functions here: https://www.theodinproject.com/paths/foundations/courses/foundations/lessons/fundamentals-part-3
28+
const number = Math.floor(Math.random() * 1000);
29+
/*The .match(/((hey))/g).length is a regex that will count the number of heys
30+
in the result, which if your function works correctly will equal the number that
31+
was randomaly generated. */
32+
expect(repeatString('hey', number).match(/((hey))/g).length).toEqual(
33+
number
34+
);
35+
});
36+
test('works with blank strings', () => {
37+
expect(repeatString('', 10)).toEqual('');
38+
});
39+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const reverseString = function (string) {
2+
return string.split("").reverse().join("");
3+
};
4+
5+
module.exports = reverseString;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const reverseString = require('./reverseString-solution');
2+
3+
describe('reverseString', () => {
4+
test('reverses single word', () => {
5+
expect(reverseString('hello')).toEqual('olleh');
6+
});
7+
8+
test('reverses multiple words', () => {
9+
expect(reverseString('hello there')).toEqual('ereht olleh');
10+
});
11+
12+
test('works with numbers and punctuation', () => {
13+
expect(reverseString('123! abc!')).toEqual('!cba !321');
14+
});
15+
test('works with blank strings', () => {
16+
expect(reverseString('')).toEqual('');
17+
});
18+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// we have 2 solutions here, an easier one and a more advanced one.
2+
// The easiest way to get an array of the rest of the arguments that are passed to a function
3+
// is using the rest operator. If this is unfamiliar to you look it up!
4+
const removeFromArray = function (array, ...args) {
5+
// create a new empty array
6+
const newArray = [];
7+
// use forEach to go through the array
8+
array.forEach((item) => {
9+
// push every element into the new array
10+
// UNLESS it is included in the function arguments
11+
// so we create a new array with every item, except those that should be removed
12+
if (!args.includes(item)) {
13+
newArray.push(item);
14+
}
15+
});
16+
// and return that array
17+
return newArray;
18+
};
19+
20+
// A simpler, but more advanced way to do it is to use the 'filter' function,
21+
// which basically does what we did with the forEach above.
22+
23+
// var removeFromArray = function(array, ...args) {
24+
// return array.filter(val => !args.includes(val))
25+
// }
26+
//
27+
28+
module.exports = removeFromArray;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const removeFromArray = require('./removeFromArray-solution');
2+
3+
describe('removeFromArray', () => {
4+
test('removes a single value', () => {
5+
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
6+
});
7+
test('removes multiple values', () => {
8+
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
9+
});
10+
test('ignores non present values', () => {
11+
expect(removeFromArray([1, 2, 3, 4], 7, 'tacos')).toEqual([1, 2, 3, 4]);
12+
});
13+
test('ignores non present values, but still works', () => {
14+
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
15+
});
16+
test('can remove all values', () => {
17+
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
18+
});
19+
test('works with strings', () => {
20+
expect(removeFromArray(['hey', 2, 3, 'ho'], 'hey', 3)).toEqual([2, 'ho']);
21+
});
22+
test('only removes same type', () => {
23+
expect(removeFromArray([1, 2, 3], '1', 3)).toEqual([1, 2]);
24+
});
25+
});

05_sumAll/solution/sumAll-solution.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const sumAll = function (min, max) {
2+
if (!Number.isInteger(min) || !Number.isInteger(max)) return "ERROR";
3+
if (min < 0 || max < 0) return "ERROR";
4+
if (min > max) {
5+
const temp = min;
6+
min = max;
7+
max = temp;
8+
}
9+
let sum = 0;
10+
for (let i = min; i < max + 1; i++) {
11+
sum += i;
12+
}
13+
return sum;
14+
};
15+
16+
module.exports = sumAll;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const sumAll = require('./sumAll-solution');
2+
3+
describe('sumAll', () => {
4+
test('sums numbers within the range', () => {
5+
expect(sumAll(1, 4)).toEqual(10);
6+
});
7+
test('works with large numbers', () => {
8+
expect(sumAll(1, 4000)).toEqual(8002000);
9+
});
10+
test('works with larger number first', () => {
11+
expect(sumAll(123, 1)).toEqual(7626);
12+
});
13+
test('returns ERROR with negative numbers', () => {
14+
expect(sumAll(-10, 4)).toEqual('ERROR');
15+
});
16+
test('returns ERROR with non-integer parameters', () => {
17+
expect(sumAll(2.5, 4)).toEqual('ERROR');
18+
});
19+
test('returns ERROR with non-number parameters', () => {
20+
expect(sumAll(10, '90')).toEqual('ERROR');
21+
});
22+
test('returns ERROR with non-number parameters', () => {
23+
expect(sumAll(10, [90, 1])).toEqual('ERROR');
24+
});
25+
});

0 commit comments

Comments
 (0)