8000 Merge pull request #234 from thatblindgeye/update_solutions · sabs/javascript-exercises@8dfb7e4 · GitHub
[go: up one dir, main page]

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+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const leapYears = function (year) {
2+
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
3+
};
4+
5+
module.exports = leapYears;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const leapYears = require('./leapYears-solution');
2+
3+
describe('leapYears', () => {
4+
test('works with non century years', () => {
5+
expect(leapYears(1996)).toBe(true);
6+
});
7+
test('works with non century years', () => {
8+
expect(leapYears(1997)).toBe(false);
9+
});
10+
test('works with ridiculously futuristic non century years', () => {
11+
expect(leapYears(34992)).toBe(true);
12+
});
13+
test('works with century years', () => {
14+
expect(leapYears(1900)).toBe(false);
15+
});
16+
test('works with century years', () => {
17+
expect(leapYears(1600)).toBe(true);
18+
});
19+
test('works with century years', () => {
20+
expect(leapYears(700)).toBe(false);
21+
});
22+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const convertToCelsius = function (fahrenheit) {
2+
return Math.round((fahrenheit - 32) * (5 / 9) * 10) / 10;
3+
};
4+
5+
const convertToFahrenheit = function (celsius) {
6+
return Math.round(((celsius * 9) / 5 + 32) * 10) / 10;
7+
};
8+
9+
module.exports = {
10+
convertToCelsius,
11+
convertToFahrenheit,
12+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const {
2+
convertToCelsius,
3+
convertToFahrenheit,
4+
} = require('./tempConversion-solution');
5+
6+
describe('convertToCelsius', () => {
7+
test('works', () => {
8+
expect(convertToCelsius(32)).toEqual(0);
9+
});
10+
test('rounds to 1 decimal', () => {
11+
expect(convertToCelsius(100)).toEqual(37.8);
12+
});
13+
test('works with negatives', () => {
14+
expect(convertToCelsius(-100)).toEqual(-73.3);
15+
});
16+
});
17+
18+
describe('convertToFahrenheit', () => {
19+
test('works', () => {
20+
expect(convertToFahrenheit(0)).toEqual(32);
21+
});
22+
test('rounds to 1 decimal', () => {
23+
expect(convertToFahrenheit(73.2)).toEqual(163.8);
24+
});
25+
test('works with negatives', () => {
26+
expect(convertToFahrenheit(-10)).toEqual(14);
27+
});
28+
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const add = function (a, b) {
2+
return a + b;
3+
};
4+
5+
const subtract = function (a, b) {
6+
return a - b;
7+
};
8+
9+
const sum = function (array) {
10+
return array.reduce((total, current) => total + current, 0);
11+
};
12+
13+
const multiply = function (array) {
14+
return array.length
15+
? array.reduce((accumulator, nextItem) => accumulator * nextItem)
16+
: 0;
17+
};
18+
19+
const power = function (a, b) {
20+
return Math.pow(a, b);
21+
};
22+
23+
const factorial = function (n) {
24+
if (n === 0) return 1;
25+
let product = 1;
26+
for (let i = n; i > 0; i--) {
27+
product *= i;
28+
}
29+
return product;
30+
};
31+
32+
// This is another implementation of Factorial that uses recursion
33+
// THANKS to @ThirtyThreeB!
34+
const recursiveFactorial = function (n) {
35+
if (n === 0) {
36+
return 1;
37+
}
38+
return n * recursiveFactorial(n - 1);
39+
};
40+
41+
module.exports = {
42+
add,
43+
subtract,
44+
sum,
45+
multiply,
46+
power,
47+
factorial,
48+
};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const calculator = require('./calculator-solution');
2+
3+
describe('add', () => {
4+
test('adds 0 and 0', () => {
5+
expect(calculator.add(0, 0)).toBe(0);
6+
});
7+
8+
test('adds 2 and 2', () => {
9+
expect(calculator.add(2, 2)).toBe(4);
10+
});
11+
12+
test('adds positive numbers', () => {
13+
expect(calculator.add(2, 6)).toBe(8);
14+
});
15+
});
16+
17+
describe('subtract', () => {
18+
test('subtracts numbers', () => {
19+
expect(calculator.subtract(10, 4)).toBe(6);
20+
});
21+
});
22+
23+
describe('sum', () => {
24+
test('computes the sum of an empty array', () => {
25+
expect(calculator.sum([])).toBe(0);
26+
});
27+
28+
test('computes the sum of an array of one number', () => {
29+
expect(calculator.sum([7])).toBe(7);
30+
});
31+
32+
test('computes the sum of an array of two numbers', () => {
33+
expect(calculator.sum([7, 11])).toBe(18);
34+
});
35+
36+
test('computes the sum of an array of many numbers', () => {
37+
expect(calculator.sum([1, 3, 5, 7, 9])).toBe(25);
38+
});
39+
});
40+
41+
describe('multiply', () => {
42+
test('multiplies two numbers', () => {
43+
expect(calculator.multiply([2, 4])).toBe(8);
44+
});
45+
46+
test('multiplies several numbers', () => {
47+
expect(calculator.multiply([2, 4, 6, 8, 10, 12, 14])).toBe(645120);
48+
});
49+
});
50+
51+
describe('power', () => {
52+
test('raises one number to the power of another number', () => {
53+
expect(calculator.power(4, 3)).toBe(64); // 4 to third power is 64
54+
});
55+
});
56+
57+
describe('factorial', () => {
58+
test('computes the factorial of 0', () => {
59+
expect(calculator.factorial(0)).toBe(1); // 0! = 1
60+
});
61+
62+
test('computes the factorial of 1', () => {
63+
expect(calculator.factorial(1)).toBe(1);
64+
});
65+
66+
test('computes the factorial of 2', () => {
67+
expect(calculator.factorial(2)).toBe(2);
68+
});
69+
70+
test('computes the factorial of 5', () => {
71+
expect(calculator.factorial(5)).toBe(120);
72+
});
73+
74+
test('computes the factorial of 10', () => {
75+
expect(calculator.factorial(10)).toBe(3628800);
76+
});
77+
});
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const palindromes = function (string) {
2+
const processedString = string.toLowerCase().replace(/[^a-z]/g, "");
3+
return processedString.split("").reverse().join("") == processedString;
4+
};
5+
6+
module.exports = palindromes;

0 commit comments

Comments
 (0)
0