diff --git a/caesar/caesar.js b/caesar/caesar.js index f4d6a25..c2e926f 100644 --- a/caesar/caesar.js +++ b/caesar/caesar.js @@ -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); -module.exports = caesar +// 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; diff --git a/calculator/calculator.js b/calculator/calculator.js index 314ee8b..fe548f7 100644 --- a/calculator/calculator.js +++ b/calculator/calculator.js @@ -1,32 +1,48 @@ -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 = { - add, - subtract, - sum, - multiply, - power, - factorial -} \ No newline at end of file + add, + subtract, + sum, + multiply, + power, + factorial +}; diff --git a/fibonacci/fibonacci.js b/fibonacci/fibonacci.js index fbf3b35..dbb906a 100644 --- a/fibonacci/fibonacci.js +++ b/fibonacci/fibonacci.js @@ -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; diff --git a/findTheOldest/findTheOldest.js b/findTheOldest/findTheOldest.js index 251a95e..80d830b 100644 --- a/findTheOldest/findTheOldest.js +++ b/findTheOldest/findTheOldest.js @@ -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 diff --git a/getTheTitles/getTheTitles.js b/getTheTitles/getTheTitles.js index 2b52aa0..d932e81 100644 --- a/getTheTitles/getTheTitles.js +++ b/getTheTitles/getTheTitles.js @@ -1,5 +1,5 @@ -const getTheTitles = function() { - +var getTheTitles = function(array) { + return array.map(book => book.title) } -module.exports = getTheTitles; +module.exports = getTheTitles diff --git a/getTheTitles/getTheTitles.spec.js b/getTheTitles/getTheTitles.spec.js index c321534..d071af4 100644 --- a/getTheTitles/getTheTitles.spec.js +++ b/getTheTitles/getTheTitles.spec.js @@ -13,7 +13,7 @@ describe('getTheTitles', () => { ] test('gets titles', () => { - expect(getTheTitles(books)).toBe(['Book','Book2']); + expect(getTheTitles(books)).toEqual(['Book','Book2']); }); }); diff --git a/helloWorld/helloWorld.js b/helloWorld/helloWorld.js index 29e76b9..9029bef 100644 --- a/helloWorld/helloWorld.js +++ b/helloWorld/helloWorld.js @@ -1,5 +1,5 @@ -const helloWorld = function() { - return '' +var helloWorld = function() { + return 'Hello, World!' } -module.exports = helloWorld; +module.exports = helloWorld diff --git a/leapYears/leapYears.js b/leapYears/leapYears.js index ac786a2..f6b3e00 100644 --- a/leapYears/leapYears.js +++ b/leapYears/leapYears.js @@ -1,5 +1,5 @@ -const leapYears = function() { - +var leapYears = function(year) { + return year % 4 === 0 && ( year % 100 !== 0 || year % 400 == 0) } module.exports = leapYears diff --git a/palindromes/palindromes.js b/palindromes/palindromes.js index b37d646..19b6aa8 100644 --- a/palindromes/palindromes.js +++ b/palindromes/palindromes.js @@ -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; diff --git a/pigLatin/pigLatin.js b/pigLatin/pigLatin.js index 6424d07..3ba465d 100644 --- a/pigLatin/pigLatin.js +++ b/pigLatin/pigLatin.js @@ -1,5 +1,21 @@ -const translate = function() { - // body... -} - -module.exports = translate \ No newline at end of file +function pigLatin(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(" "); + } + + 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 \ No newline at end of file diff --git a/pigLatin/pigLatin.spec.js b/pigLatin/pigLatin.spec.js index 7936719..abd53d3 100644 --- a/pigLatin/pigLatin.spec.js +++ b/pigLatin/pigLatin.spec.js @@ -19,38 +19,38 @@ const pigLatin = require('./pigLatin') describe('translate', () => { 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', () => { - expect(pigLatin.translate("banana")).toBe("ananabay"); + expect(pigLatin("banana")).toBe("ananabay"); }); 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', () => { - expect(pigLatin.translate("eat pie")).toBe('eatay iepay'); + expect(pigLatin("eat pie")).toBe('eatay iepay'); }); 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', () => { - expect(pigLatin.translate("school")).toBe("oolschay"); + expect(pigLatin("school")).toBe("oolschay"); }); 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', () => { - expect(pigLatin.translate("square")).toBe("aresquay"); + expect(pigLatin("square")).toBe("aresquay"); }); 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"); }); }); diff --git a/removeFromArray/removeFromArray.js b/removeFromArray/removeFromArray.js index 11efd5c..8bf214e 100644 --- a/removeFromArray/removeFromArray.js +++ b/removeFromArray/removeFromArray.js @@ -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; diff --git a/removeFromArray/removeFromArray.spec.js b/removeFromArray/removeFromArray.spec.js index efd1415..21f34cf 100644 --- a/removeFromArray/removeFromArray.spec.js +++ b/removeFromArray/removeFromArray.spec.js @@ -2,24 +2,24 @@ const removeFromArray = require('./removeFromArray') describe('removeFromArray', () => { 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', () => { - 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', () => { - 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', () => { - 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', () => { - 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', () => { - 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', () => { - expect(removeFromArray([1, 2, 3], "1", 3)).toBe([1, 2]); + expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]); }); }); diff --git a/repeatString/repeatString.js b/repeatString/repeatString.js index 770119a..b300278 100644 --- a/repeatString/repeatString.js +++ b/repeatString/repeatString.js @@ -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 diff --git a/reverseString/reverseString.js b/reverseString/reverseString.js index febb577..bbab5c1 100644 --- a/reverseString/reverseString.js +++ b/reverseString/reverseString.js @@ -1,5 +1,5 @@ -const reverseString = function() { - +var reverseString = function(string) { + return string.split('').reverse().join('') } module.exports = reverseString diff --git a/snakeCase/snakeCase.js b/snakeCase/snakeCase.js index c948201..4dc3af3 100644 --- a/snakeCase/snakeCase.js +++ b/snakeCase/snakeCase.js @@ -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; diff --git a/sumAll/sumAll.js b/sumAll/sumAll.js index 4030fe8..785abdb 100644 --- a/sumAll/sumAll.js +++ b/sumAll/sumAll.js @@ -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; diff --git a/tempConversion/tempConversion.js b/tempConversion/tempConversion.js index 4fa21ee..742d8e2 100644 --- a/tempConversion/tempConversion.js +++ b/tempConversion/tempConversion.js @@ -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 = {