Skip to content

Commit

Permalink
Merge pull request TheOdinProject#234 from thatblindgeye/update_solut…
Browse files Browse the repository at this point in the history
…ions

Exercises: Add solution directories for all exercises
  • Loading branch information
thatblindgeye authored Feb 20, 2023
2 parents 8746ce0 + e416717 commit 8dfb7e4
Show file tree
Hide file tree
Showing 37 changed files with 734 additions and 9 deletions.
5 changes: 5 additions & 0 deletions 01_helloWorld/solution/helloWorld-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const helloWorld = function () {
return "Hello, World!";
};

module.exports = helloWorld;
7 changes: 7 additions & 0 deletions 01_helloWorld/solution/helloWorld-solution.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const helloWorld = require('./helloWorld-solution');

describe('Hello World', function () {
test('says "Hello, World!"', function () {
expect(helloWorld()).toEqual('Hello, World!');
});
});
10 changes: 10 additions & 0 deletions 02_repeatString/solution/repeatString-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const repeatString = function (word, times) {
if (times < 0) return "ERROR";
let string = "";
for (let i = 0; i < times; i++) {
string += word;
}
return string;
};

module.exports = repeatString;
39 changes: 39 additions & 0 deletions 02_repeatString/solution/repeatString-solution.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const repeatString = require('./repeatString-solution');

describe('repeatString', () => {
test('repeats the string', () => {
expect(repeatString('hey', 3)).toEqual('heyheyhey');
});
test('repeats the string many times', () => {
expect(repeatString('hey', 10)).toEqual('heyheyheyheyheyheyheyheyheyhey');
});
test('repeats the string 1 times', () => {
expect(repeatString('hey', 1)).toEqual('hey');
});
test('repeats the string 0 times', () => {
expect(repeatString('hey', 0)).toEqual('');
});
test('returns ERROR with negative numbers', () => {
expect(repeatString('hey', -1)).toEqual('ERROR');
});
test('repeats the string a random amount of times', function () {
/*The number is generated by using Math.random to get a value from between
0 to 1, when this is multiplied by 1000 and rounded down with Math.floor it
equals a number between 0 to 999 (this number will change everytime you run
the test).*/

// DO NOT use Math.floor(Math.random() * 1000) in your code,
// this test generates a random number, then passes it into your code with a function parameter.
// 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
const number = Math.floor(Math.random() * 1000);
/*The .match(/((hey))/g).length is a regex that will count the number of heys
in the result, which if your function works correctly will equal the number that
was randomaly generated. */
expect(repeatString('hey', number).match(/((hey))/g).length).toEqual(
number
);
});
test('works with blank strings', () => {
expect(repeatString('', 10)).toEqual('');
});
});
5 changes: 5 additions & 0 deletions 03_reverseString/solution/reverseString-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const reverseString = function (string) {
return string.split("").reverse().join("");
};

module.exports = reverseString;
18 changes: 18 additions & 0 deletions 03_reverseString/solution/reverseString-solution.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const reverseString = require('./reverseString-solution');

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

test('reverses multiple words', () => {
expect(reverseString('hello there')).toEqual('ereht olleh');
});

test('works with numbers and punctuation', () => {
expect(reverseString('123! abc!')).toEqual('!cba !321');
});
test('works with blank strings', () => {
expect(reverseString('')).toEqual('');
});
});
28 changes: 28 additions & 0 deletions 04_removeFromArray/solution/removeFromArray-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// we have 2 solutions here, an easier one and a more advanced one.
// The easiest way to get an array of the rest of the arguments that are passed to a function
// is using the rest operator. If this is unfamiliar to you look it up!
const removeFromArray = function (array, ...args) {
// create a new empty array
const newArray = [];
// use forEach to go through the array
array.forEach((item) => {
// push every element into the new array
// UNLESS it is included in the function arguments
// so we create a new array with every item, except those that should be removed
if (!args.includes(item)) {
newArray.push(item);
}
});
// and return that array
return newArray;
};

// A simpler, but more advanced way to do it is to use the 'filter' function,
// which basically does what we did with the forEach above.

// var removeFromArray = function(array, ...args) {
// return array.filter(val => !args.includes(val))
// }
//

module.exports = removeFromArray;
25 changes: 25 additions & 0 deletions 04_removeFromArray/solution/removeFromArray-solution.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const removeFromArray = require('./removeFromArray-solution');

describe('removeFromArray', () => {
test('removes a single value', () => {
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
});
test('removes multiple values', () => {
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
});
test('ignores non present values', () => {
expect(removeFromArray([1, 2, 3, 4], 7, 'tacos')).toEqual([1, 2, 3, 4]);
});
test('ignores non present values, but still works', () => {
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
});
test('can remove all values', () => {
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
});
test('works with strings', () => {
expect(removeFromArray(['hey', 2, 3, 'ho'], 'hey', 3)).toEqual([2, 'ho']);
});
test('only removes same type', () => {
expect(removeFromArray([1, 2, 3], '1', 3)).toEqual([1, 2]);
});
});
16 changes: 16 additions & 0 deletions 05_sumAll/solution/sumAll-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const sumAll = function (min, max) {
if (!Number.isInteger(min) || !Number.isInteger(max)) return "ERROR";
if (min < 0 || max < 0) return "ERROR";
if (min > max) {
const temp = min;
min = max;
max = temp;
}
let sum = 0;
for (let i = min; i < max + 1; i++) {
sum += i;
}
return sum;
};

module.exports = sumAll;
25 changes: 25 additions & 0 deletions 05_sumAll/solution/sumAll-solution.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const sumAll = require('./sumAll-solution');

describe('sumAll', () => {
test('sums numbers within the range', () => {
expect(sumAll(1, 4)).toEqual(10);
});
test('works with large numbers', () => {
expect(sumAll(1, 4000)).toEqual(8002000);
});
test('works with larger number first', () => {
expect(sumAll(123, 1)).toEqual(7626);
});
test('returns ERROR with negative numbers', () => {
expect(sumAll(-10, 4)).toEqual('ERROR');
});
test('returns ERROR with non-integer parameters', () => {
expect(sumAll(2.5, 4)).toEqual('ERROR');
});
test('returns ERROR with non-number parameters', () => {
expect(sumAll(10, '90')).toEqual('ERROR');
});
test('returns ERROR with non-number parameters', () => {
expect(sumAll(10, [90, 1])).toEqual('ERROR');
});
});
5 changes: 5 additions & 0 deletions 06_leapYears/solution/leapYears-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const leapYears = function (year) {
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
};

module.exports = leapYears;
22 changes: 22 additions & 0 deletions 06_leapYears/solution/leapYears-solution.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const leapYears = require('./leapYears-solution');

describe('leapYears', () => {
test('works with non century years', () => {
expect(leapYears(1996)).toBe(true);
});
test('works with non century years', () => {
expect(leapYears(1997)).toBe(false);
});
test('works with ridiculously futuristic non century years', () => {
expect(leapYears(34992)).toBe(true);
});
test('works with century years', () => {
expect(leapYears(1900)).toBe(false);
});
test('works with century years', () => {
expect(leapYears(1600)).toBe(true);
});
test('works with century years', () => {
expect(leapYears(700)).toBe(false);
});
});
12 changes: 12 additions & 0 deletions 07_tempConversion/solution/tempConversion-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const convertToCelsius = function (fahrenheit) {
return Math.round((fahrenheit - 32) * (5 / 9) * 10) / 10;
};

const convertToFahrenheit = function (celsius) {
return Math.round(((celsius * 9) / 5 + 32) * 10) / 10;
};

module.exports = {
convertToCelsius,
convertToFahrenheit,
};
28 changes: 28 additions & 0 deletions 07_tempConversion/solution/tempConversion-solution.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const {
convertToCelsius,
convertToFahrenheit,
} = require('./tempConversion-solution');

describe('convertToCelsius', () => {
test('works', () => {
expect(convertToCelsius(32)).toEqual(0);
});
test('rounds to 1 decimal', () => {
expect(convertToCelsius(100)).toEqual(37.8);
});
test('works with negatives', () => {
expect(convertToCelsius(-100)).toEqual(-73.3);
});
});

describe('convertToFahrenheit', () => {
test('works', () => {
expect(convertToFahrenheit(0)).toEqual(32);
});
test('rounds to 1 decimal', () => {
expect(convertToFahrenheit(73.2)).toEqual(163.8);
});
test('works with negatives', () => {
expect(convertToFahrenheit(-10)).toEqual(14);
});
});
48 changes: 48 additions & 0 deletions 08_calculator/solution/calculator-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const add = function (a, b) {
return a + b;
};

const subtract = function (a, b) {
return a - b;
};

const sum = function (array) {
return array.reduce((total, current) => total + current, 0);
};

const multiply = function (array) {
return array.length
? array.reduce((accumulator, nextItem) => accumulator * nextItem)
: 0;
};

const power = function (a, b) {
return Math.pow(a, b);
};

const factorial = function (n) {
if (n === 0) return 1;
let product = 1;
for (let i = n; i > 0; i--) {
product *= i;
}
return product;
};

// This is another implementation of Factorial that uses recursion
// THANKS to @ThirtyThreeB!
const recursiveFactorial = function (n) {
if (n === 0) {
return 1;
}
return n * recursiveFactorial(n - 1);
};

module.exports = {
add,
subtract,
sum,
multiply,
power,
factorial,
};
77 changes: 77 additions & 0 deletions 08_calculator/solution/calculator-solution.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const calculator = require('./calculator-solution');

describe('add', () => {
test('adds 0 and 0', () => {
expect(calculator.add(0, 0)).toBe(0);
});

test('adds 2 and 2', () => {
expect(calculator.add(2, 2)).toBe(4);
});

test('adds positive numbers', () => {
expect(calculator.add(2, 6)).toBe(8);
});
});

describe('subtract', () => {
test('subtracts numbers', () => {
expect(calculator.subtract(10, 4)).toBe(6);
});
});

describe('sum', () => {
test('computes the sum of an empty array', () => {
expect(calculator.sum([])).toBe(0);
});

test('computes the sum of an array of one number', () => {
expect(calculator.sum([7])).toBe(7);
});

test('computes the sum of an array of two numbers', () => {
expect(calculator.sum([7, 11])).toBe(18);
});

test('computes the sum of an array of many numbers', () => {
expect(calculator.sum([1, 3, 5, 7, 9])).toBe(25);
});
});

describe('multiply', () => {
test('multiplies two numbers', () => {
expect(calculator.multiply([2, 4])).toBe(8);
});

test('multiplies several numbers', () => {
expect(calculator.multiply([2, 4, 6, 8, 10, 12, 14])).toBe(645120);
});
});

describe('power', () => {
test('raises one number to the power of another number', () => {
expect(calculator.power(4, 3)).toBe(64); // 4 to third power is 64
});
});

describe('factorial', () => {
test('computes the factorial of 0', () => {
expect(calculator.factorial(0)).toBe(1); // 0! = 1
});

test('computes the factorial of 1', () => {
expect(calculator.factorial(1)).toBe(1);
});

test('computes the factorial of 2', () => {
expect(calculator.factorial(2)).toBe(2);
});

test('computes the factorial of 5', () => {
expect(calculator.factorial(5)).toBe(120);
});

test('computes the factorial of 10', () => {
expect(calculator.factorial(10)).toBe(3628800);
});
});
6 changes: 6 additions & 0 deletions 09_palindromes/solution/palindromes-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const palindromes = function (string) {
const processedString = string.toLowerCase().replace(/[^a-z]/g, "");
return processedString.split("").reverse().join("") == processedString;
};

module.exports = palindromes;
Loading

0 comments on commit 8dfb7e4

Please sign in to comment.