Tweak matchers in removeFromArray, pull pigLatin solution from old branch
This commit is contained in:
parent
febe6522f3
commit
749a48adee
|
@ -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
|
module.exports = caesar;
|
||||||
|
|
|
@ -1,25 +1,41 @@
|
||||||
const add = function() {
|
function add(a, b) {
|
||||||
|
return a + b;
|
||||||
}
|
}
|
||||||
|
|
||||||
const subtract = function() {
|
function subtract(a, b) {
|
||||||
|
return a - b;
|
||||||
}
|
}
|
||||||
|
|
||||||
const sum = function() {
|
function sum(array) {
|
||||||
|
return array.reduce((total, current) => total + current, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
const multiply = function() {
|
function multiply(array) {
|
||||||
|
return array.length
|
||||||
|
? array.reduce((accumulator, nextItem) => accumulator * nextItem)
|
||||||
|
: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const power = function() {
|
function power(a, b) {
|
||||||
|
return Math.pow(a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
const factorial = function() {
|
function factorial(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!
|
||||||
|
function recursiveFactorial(n) {
|
||||||
|
if (n===0){
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return n * recursiveFactorial (n-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
@ -29,4 +45,4 @@ module.exports = {
|
||||||
multiply,
|
multiply,
|
||||||
power,
|
power,
|
||||||
factorial
|
factorial
|
||||||
}
|
};
|
||||||
|
|
|
@ -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;
|
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
|
module.exports = findTheOldest
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const getTheTitles = function() {
|
var getTheTitles = function(array) {
|
||||||
|
return array.map(book => book.title)
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = getTheTitles;
|
module.exports = getTheTitles
|
||||||
|
|
|
@ -13,7 +13,7 @@ describe('getTheTitles', () => {
|
||||||
]
|
]
|
||||||
|
|
||||||
test('gets titles', () => {
|
test('gets titles', () => {
|
||||||
expect(getTheTitles(books)).toBe(['Book','Book2']);
|
expect(getTheTitles(books)).toEqual(['Book','Book2']);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const helloWorld = function() {
|
var helloWorld = function() {
|
||||||
return ''
|
return 'Hello, World!'
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = helloWorld;
|
module.exports = helloWorld
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const leapYears = function() {
|
var leapYears = function(year) {
|
||||||
|
return year % 4 === 0 && ( year % 100 !== 0 || year % 400 == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = leapYears
|
module.exports = leapYears
|
||||||
|
|
|
@ -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;
|
module.exports = palindromes;
|
||||||
|
|
|
@ -1,5 +1,21 @@
|
||||||
const translate = function() {
|
function pigLatin(string) {
|
||||||
// body...
|
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(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = translate
|
function firstVowelIndex(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
|
|
@ -19,38 +19,38 @@ const pigLatin = require('./pigLatin')
|
||||||
|
|
||||||
describe('translate', () => {
|
describe('translate', () => {
|
||||||
test('translates a word beginning with a vowel', () => {
|
test('translates a word beginning with a vowel', () => {
|
||||||
expect(pigLatin.translate("apple")).toBe('appleay');
|
expect(pigLatin("apple")).toBe('appleay');
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('translates a word beginning with a consonant', () => {
|
test.skip('translates a word beginning with a consonant', () => {
|
||||||
expect(pigLatin.translate("banana")).toBe("ananabay");
|
expect(pigLatin("banana")).toBe("ananabay");
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('translates a word beginning with two consonants', () => {
|
test.skip('translates a word beginning with two consonants', () => {
|
||||||
expect(pigLatin.translate("cherry")).toBe('errychay');
|
expect(pigLatin("cherry")).toBe('errychay');
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('translates two words', () => {
|
test.skip('translates two words', () => {
|
||||||
expect(pigLatin.translate("eat pie")).toBe('eatay iepay');
|
expect(pigLatin("eat pie")).toBe('eatay iepay');
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('translates a word beginning with three consonants', () => {
|
test.skip('translates a word beginning with three consonants', () => {
|
||||||
expect(pigLatin.translate("three")).toBe("eethray");
|
expect(pigLatin("three")).toBe("eethray");
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('counts "sch" as a single phoneme', () => {
|
test.skip('counts "sch" as a single phoneme', () => {
|
||||||
expect(pigLatin.translate("school")).toBe("oolschay");
|
expect(pigLatin("school")).toBe("oolschay");
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('counts "qu" as a single phoneme', () => {
|
test.skip('counts "qu" as a single phoneme', () => {
|
||||||
expect(pigLatin.translate("quiet")).toBe("ietquay");
|
expect(pigLatin("quiet")).toBe("ietquay");
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('counts "qu" as a consonant even when its preceded by a consonant', () => {
|
test.skip('counts "qu" as a consonant even when its preceded by a consonant', () => {
|
||||||
expect(pigLatin.translate("square")).toBe("aresquay");
|
expect(pigLatin("square")).toBe("aresquay");
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('translates many words', () => {
|
test.skip('translates many words', () => {
|
||||||
expect(pigLatin.translate("the quick brown fox")).toBe("ethay ickquay ownbray oxfay");
|
expect(pigLatin("the quick brown fox")).toBe("ethay ickquay ownbray oxfay");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,32 @@
|
||||||
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;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = removeFromArray
|
|
||||||
|
// 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;
|
||||||
|
|
|
@ -2,24 +2,24 @@ const removeFromArray = require('./removeFromArray')
|
||||||
|
|
||||||
describe('removeFromArray', () => {
|
describe('removeFromArray', () => {
|
||||||
test('removes a single value', () => {
|
test('removes a single value', () => {
|
||||||
expect(removeFromArray([1, 2, 3, 4], 3)).toBe([1, 2, 4]);
|
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
|
||||||
});
|
});
|
||||||
test.skip('removes multiple values', () => {
|
test.skip('removes multiple values', () => {
|
||||||
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toBe([1, 4]);
|
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
|
||||||
});
|
});
|
||||||
test.skip('ignores non present values', () => {
|
test.skip('ignores non present values', () => {
|
||||||
expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toBe([1, 2, 3, 4]);
|
expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]);
|
||||||
});
|
});
|
||||||
test.skip('ignores non present values, but still works', () => {
|
test.skip('ignores non present values, but still works', () => {
|
||||||
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toBe([1, 3, 4]);
|
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
|
||||||
});
|
});
|
||||||
test.skip('can remove all values', () => {
|
test.skip('can remove all values', () => {
|
||||||
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toBe([]);
|
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
|
||||||
});
|
});
|
||||||
test.skip('works with strings', () => {
|
test.skip('works with strings', () => {
|
||||||
expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toBe([2, "ho"]);
|
expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
|
||||||
});
|
});
|
||||||
test.skip('only removes same type', () => {
|
test.skip('only removes same type', () => {
|
||||||
expect(removeFromArray([1, 2, 3], "1", 3)).toBe([1, 2]);
|
expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
const repeatString = function() {
|
var 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
|
module.exports = repeatString
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const reverseString = function() {
|
var reverseString = function(string) {
|
||||||
|
return string.split('').reverse().join('')
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = reverseString
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = snakeCase
|
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
|
module.exports = sumAll;
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
const ftoc = function() {
|
var ftoc = function(f) {
|
||||||
|
return Math.round((f - 32) * (5/9) * 10) / 10
|
||||||
}
|
}
|
||||||
|
|
||||||
const ctof = function() {
|
var ctof = function(c) {
|
||||||
|
return Math.round(((c * 9/5) + 32) * 10) / 10
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
Loading…
Reference in New Issue