Merge pull request #150 from TheOdinProject/jest-solutions
Implement Jest testing platform for all exercises (Solutions branch)
This commit is contained in:
commit
4fca2e249c
|
@ -1,5 +1,26 @@
|
|||
const caesar = function() {
|
||||
const caesar = function(string, shift) {
|
||||
return string
|
||||
.split("")
|
||||
.map(char => shiftChar(char, shift))
|
||||
.join("");
|
||||
};
|
||||
|
||||
const codeSet = code => (code < 97 ? 65 : 97);
|
||||
|
||||
// this function is just a fancy way of doing % so that it works with negative numbers
|
||||
// see this link for details:
|
||||
// https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers
|
||||
const mod = (n, m) => (n % m + m) % m;
|
||||
|
||||
const shiftChar = (char, shift) => {
|
||||
const code = char.charCodeAt();
|
||||
|
||||
if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
|
||||
return String.fromCharCode(
|
||||
mod(code + shift - codeSet(code), 26) + codeSet(code)
|
||||
);
|
||||
}
|
||||
return char;
|
||||
};
|
||||
|
||||
module.exports = caesar;
|
||||
|
|
|
@ -1,25 +1,41 @@
|
|||
const add = function() {
|
||||
|
||||
const add = function(a, b) {
|
||||
return a + b;
|
||||
};
|
||||
|
||||
const subtract = function() {
|
||||
|
||||
const subtract = function(a, b) {
|
||||
return a - b;
|
||||
};
|
||||
|
||||
const sum = function() {
|
||||
|
||||
const sum = function(array) {
|
||||
return array.reduce((total, current) => total + current, 0);
|
||||
};
|
||||
|
||||
const multiply = function() {
|
||||
|
||||
const multiply = function(array) {
|
||||
return array.length
|
||||
? array.reduce((accumulator, nextItem) => accumulator * nextItem)
|
||||
: 0;
|
||||
};
|
||||
|
||||
const power = function() {
|
||||
|
||||
const power = function(a, b) {
|
||||
return Math.pow(a, b);
|
||||
};
|
||||
|
||||
const factorial = function() {
|
||||
|
||||
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 = {
|
||||
|
|
|
@ -1,5 +1,14 @@
|
|||
const fibonacci = function() {
|
||||
|
||||
const fibonacci = function(count) {
|
||||
if (count < 0) return "OOPS";
|
||||
if (count === 0) return 0;
|
||||
let a = 0;
|
||||
let b = 1;
|
||||
for (let i = 1; i < count; i++) {
|
||||
const temp = b;
|
||||
b = a + b;
|
||||
a = temp;
|
||||
}
|
||||
return b;
|
||||
};
|
||||
|
||||
module.exports = fibonacci;
|
||||
|
|
|
@ -1,5 +1,16 @@
|
|||
const findTheOldest = function() {
|
||||
const findTheOldest = function(array) {
|
||||
return array.reduce((oldest, currentPerson) => {
|
||||
const oldestAge = getAge(oldest.yearOfBirth, oldest.yearOfDeath)
|
||||
const currentAge = getAge(currentPerson.yearOfBirth, currentPerson.yearOfDeath)
|
||||
return oldestAge < currentAge ? currentPerson : oldest
|
||||
})
|
||||
};
|
||||
|
||||
const getAge = function(birth, death) {
|
||||
if (!death) {
|
||||
death = new Date().getFullYear();
|
||||
}
|
||||
return death - birth;
|
||||
};
|
||||
|
||||
module.exports = findTheOldest;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const getTheTitles = function() {
|
||||
|
||||
const getTheTitles = function(array) {
|
||||
return array.map(book => book.title)
|
||||
};
|
||||
|
||||
module.exports = getTheTitles;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const helloWorld = function() {
|
||||
return ''
|
||||
return 'Hello, World!'
|
||||
};
|
||||
|
||||
module.exports = helloWorld;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const leapYears = function() {
|
||||
|
||||
const leapYears = function(year) {
|
||||
return year % 4 === 0 && ( year % 100 !== 0 || year % 400 === 0)
|
||||
};
|
||||
|
||||
module.exports = leapYears;
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -14,11 +14,11 @@
|
|||
},
|
||||
"homepage": "https://github.com/TheOdinProject/javascript-exercises#readme",
|
||||
"devDependencies": {
|
||||
"jest": "^26.6.3",
|
||||
"jest-cli": "^26.6.3",
|
||||
"eslint": "^7.26.0",
|
||||
"eslint-config-airbnb-base": "^14.2.1",
|
||||
"eslint-plugin-import": "^2.22.1"
|
||||
"eslint-plugin-import": "^2.22.1",
|
||||
"jest": "^26.6.3",
|
||||
"jest-cli": "^26.6.3"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "jest"
|
||||
|
|
|
@ -1,5 +1,11 @@
|
|||
const palindromes = function () {};
|
||||
|
||||
const palindromes = function(string) {
|
||||
processedString = string.toLowerCase().replace(/[^A-Za-z]/g, "");
|
||||
return (
|
||||
processedString
|
||||
.split("")
|
||||
.reverse()
|
||||
.join("") == processedString
|
||||
);
|
||||
};
|
||||
|
||||
module.exports = palindromes;
|
||||
|
|
|
@ -1,5 +1,21 @@
|
|||
function pigLatin(string) {
|
||||
const pigLatin = function(string) {
|
||||
return string
|
||||
.split(" ")
|
||||
.map(word => {
|
||||
const index = firstVowelIndex(word);
|
||||
const beginning = word.slice(0, index);
|
||||
const ending = word.slice(index);
|
||||
return `${ending}${beginning}ay`;
|
||||
})
|
||||
.join(" ");
|
||||
};
|
||||
|
||||
const firstVowelIndex = function(string) {
|
||||
const vowels = string.match(/[aeiou]/g);
|
||||
if (vowels[0] == "u" && string[string.indexOf(vowels[0]) - 1] == "q") {
|
||||
return string.indexOf(vowels[1]);
|
||||
}
|
||||
return string.indexOf(vowels[0]);
|
||||
};
|
||||
|
||||
|
||||
module.exports = pigLatin;
|
||||
|
|
|
@ -1,5 +1,31 @@
|
|||
const removeFromArray = function() {
|
||||
|
||||
// we have 2 solutions here, an easier one and a more advanced one.
|
||||
// The easiest way to get an array of all of the arguments that are passed to a function
|
||||
// is using the spread operator. If this is unfamiliar to you look it up!
|
||||
const removeFromArray = function (...args) {
|
||||
// the very first item in our list of arguments is the array, we pull it out with args[0]
|
||||
const array = args[0];
|
||||
// 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(...args) {
|
||||
// const array = args[0]
|
||||
// return array.filter(val => !args.includes(val))
|
||||
// }
|
||||
//
|
||||
|
||||
module.exports = removeFromArray;
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
const repeatString = function() {
|
||||
|
||||
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;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const reverseString = function() {
|
||||
|
||||
const reverseString = function(string) {
|
||||
return string.split('').reverse().join('')
|
||||
};
|
||||
|
||||
module.exports = reverseString;
|
||||
|
|
|
@ -1,5 +1,19 @@
|
|||
const snakeCase = function() {
|
||||
const snakeCase = function(string) {
|
||||
// wtf case
|
||||
string = string.replace(/\.\./g, " ");
|
||||
|
||||
// this splits up camelcase IF there are no spaces in the word
|
||||
if (string.indexOf(" ") < 0) {
|
||||
string = string.replace(/([A-Z])/g, " $1");
|
||||
}
|
||||
|
||||
return string
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/[,\?\.]/g, "")
|
||||
.replace(/\-/g, " ")
|
||||
.split(" ")
|
||||
.join("_");
|
||||
};
|
||||
|
||||
module.exports = snakeCase;
|
||||
|
|
|
@ -1,5 +1,16 @@
|
|||
const sumAll = function() {
|
||||
|
||||
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;
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
const ftoc = function() {
|
||||
|
||||
const ftoc = function(f) {
|
||||
return Math.round((f - 32) * (5/9) * 10) / 10
|
||||
};
|
||||
|
||||
const ctof = function() {
|
||||
|
||||
const ctof = function(c) {
|
||||
return Math.round(((c * 9/5) + 32) * 10) / 10
|
||||
};
|
||||
|
||||
|
||||
module.exports = {
|
||||
ftoc,
|
||||
ctof
|
||||
|
|
Loading…
Reference in New Issue