Skip to content

Commit fb1a2db

Browse files
author
thatblindgeye
committed
Add solution directories for exercises
1 parent 1971712 commit fb1a2db

26 files changed

+600
-0
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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.skip('repeats the string many times', () => {
8+
expect(repeatString('hey', 10)).toEqual(
9+
'heyheyheyheyheyheyheyheyheyhey'
10+
);
11+
});
12+
test.skip('repeats the string 1 times', () => {
13+
expect(repeatString('hey', 1)).toEqual('hey');
14+
});
15+
test.skip('repeats the string 0 times', () => {
16+
expect(repeatString('hey', 0)).toEqual('');
17+
});
18+
test.skip('returns ERROR with negative numbers', () => {
19+
expect(repeatString('hey', -1)).toEqual('ERROR');
20+
});
21+
test.skip('repeats the string a random amount of times', function () {
22+
/*The number is generated by using Math.random to get a value from between
23+
0 to 1, when this is multiplied by 1000 and rounded down with Math.floor it
24+
equals a number between 0 to 999 (this number will change everytime you run
25+
the test).*/
26+
27+
// DO NOT use Math.floor(Math.random() * 1000) in your code,
28+
// this test generates a random number, then passes it into your code with a function parameter.
29+
// 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
30+
const number = Math.floor(Math.random() * 1000);
31+
/*The .match(/((hey))/g).length is a regex that will count the number of heys
32+
in the result, which if your function works correctly will equal the number that
33+
was randomaly generated. */
34+
expect(repeatString('hey', number).match(/((hey))/g).length).toEqual(
35+
number
36+
);
37+
});
38+
test.skip('works with blank strings', () => {
39+
expect(repeatString('', 10)).toEqual('');
40+
});
41+
});
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.skip('reverses multiple words', () => {
9+
expect(reverseString('hello there')).toEqual('ereht olleh');
10+
});
11+
12+
test.skip('works with numbers and punctuation', () => {
13+
expect(reverseString('123! abc!')).toEqual('!cba !321');
14+
});
15+
test.skip('works with blank strings', () => {
16+
expect(reverseString('')).toEqual('');
17+
});
18+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// we have 2 solutions here, an easier one and a more advanced one.
2+
// The easiest way to get an array of all 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 (...args) {
5+
// the very first item in our list of arguments is the array, we pull it out with args[0]
6+
const array = args[0];
7+
// create a new empty array
8+
const newArray = [];
9+
// use forEach to go through the array
10+
array.forEach((item) => {
11+
// push every element into the new array
12+
// UNLESS it is included in the function arguments
13+
// so we create a new array with every item, except those that should be removed
14+
if (!args.includes(item)) {
15+
newArray.push(item);
16+
}
17+
});
18+
// and return that array
19+
return newArray;
20+
};
21+
22+
// A simpler, but more advanced way to do it is to use the 'filter' function,
23+
// which basically does what we did with the forEach above.
24+
25+
// var removeFromArray = function(...args) {
26+
// const array = args[0]
27+
// return array.filter(val => !args.includes(val))
28+
// }
29+
//
30+
31+
module.exports = removeFromArray;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.skip('removes multiple values', () => {
8+
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
9+
});
10+
test.skip('ignores non present values', () => {
11+
expect(removeFromArray([1, 2, 3, 4], 7, 'tacos')).toEqual([1, 2, 3, 4]);
12+
});
13+
test.skip('ignores non present values, but still works', () => {
14+
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
15+
});
16+
test.skip('can remove all values', () => {
17+
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
18+
});
19+
test.skip('works with strings', () => {
20+
expect(removeFromArray(['hey', 2, 3, 'ho'], 'hey', 3)).toEqual([
21+
2,
22+
'ho',
23+
]);
24+
});
25+
test.skip('only removes same type', () => {
26+
expect(removeFromArray([1, 2, 3], '1', 3)).toEqual([1, 2]);
27+
});
28+
});

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.skip('works with large numbers', () => {
8+
expect(sumAll(1, 4000)).toEqual(8002000);
9+
});
10+
test.skip('works with larger number first', () => {
11+
expect(sumAll(123, 1)).toEqual(7626);
12+
});
13+
test.skip('returns ERROR with negative numbers', () => {
14+
expect(sumAll(-10, 4)).toEqual('ERROR');
15+
});
16+
test.skip('returns ERROR with non-number parameters', () => {
17+
expect(sumAll(10, '90')).toEqual('ERROR');
18+
});
19+
test.skip('returns ERROR with non-number parameters', () => {
20+
expect(sumAll(10, [90, 1])).toEqual('ERROR');
21+
});
22+
});

0 commit comments

Comments
 (0)