diff --git a/README.md b/README.md index 44a38be..50fe005 100644 --- a/README.md +++ b/README.md @@ -73,8 +73,8 @@ You don't have to write an answer to the thought questions. **Thought Questions** -- What happened to the original array? -- Can you store the values from a `forEach` method in a new array? +- What happened to the original array? nothing +- Can you store the values from a `forEach` method in a new array? yes #### Map @@ -83,9 +83,9 @@ You don't have to write an answer to the thought questions. **Thought Questions** -- What happened to the original array? +- What happened to the original array? nothing - Can you store the values from a `map` method in a new array? - +yes #### Some - Find out if some numbers are divisible by 7 diff --git a/solution.js b/solution.js index 049257f..c7ffed4 100644 --- a/solution.js +++ b/solution.js @@ -1,74 +1,124 @@ const { nums, words } = require("./data/data.js"); -// Every -const isEveryNumGreaterThan2 = () => { +// Every returns all instances +const isEveryNumGreaterThan2 = (nums) => { + return nums.every(num => num > 2) // }; -const isEveryWordShorterThan7 = () => { +const isEveryWordShorterThan7 = (words) => { + return words.every(word => word.length < 7) + // }; -// Filter - -const arrayLessThan5 = () => { +// Filter array by a condition +const arrayLessThan5 = (nums) => { + return nums.filter(num => num < 5) // }; -const arrayOddLengthWords = () => { +const arrayOddLengthWords = (words) => { + return words.filter(word => word.length % 2 > 0) // }; -// Find +// Find the first instance of a condition +const firstValDivisibleBy4 = (nums) => { + return nums.find(num => num % 4 === 0) + // +}; -const firstValDivisibleBy4 = () => { +const firstWordLongerThan4Char = (words) => { + return words.find(word => word.length > 4) // }; -const firstWordLongerThan4Char = () => { +// Find Index first index that matches criteria +const firstNumIndexDivisibleBy3 = (nums) => { + return nums.findIndex(num => num % 3 === 0) // }; -// Find Index +const firstWordIndexLessThan2Char = (words) => { + return words.findIndex(word => word.length < 2) + // +}; -const firstNumIndexDivisibleBy3 = () => { +// For Each applies criteria to each element +const logValuesTimes3 = (nums) => { + return nums.forEach(num => num * 3) // }; -const firstWordIndexLessThan2Char = () => { +const logWordsWithExclamation = (words) => { + return words.forEach(word => word + "!") // }; -// For Each +// Map return a new array with a mutation applied +const arrayValuesSquaredTimesIndex = (nums) => { + return nums.map((num, index)=> num * num * index) -const logValuesTimes3 = () => { // }; -const logWordsWithExclamation = () => { +const arrayWordsUpcased = (words) => { + return words.map(word => word.toUpperCase()) // }; -// Map - -const arrayValuesSquaredTimesIndex = () => { +// Some returns all one instance of criteria +const areSomeNumsDivisibleBy7 = (nums) => { + return nums.some(num => num % 7 === 0) // }; -const arrayWordsUpcased = () => { +const doSomeWordsHaveAnA = (words) => { + return words.some(word => word.toLowerCase().includes("a")) // }; -// Some +const totalOfNumbersInArray = (nums) => { + return nums.reduce((acc, num) => acc + num, 0) +}; +totalOfNumbersInArray(nums) -const areSomeNumsDivisibleBy7 = () => { - // +const concatenateAllTheWords = (words) => { + return words.reduce((acc, word) => acc + word, ""); }; -const doSomeWordsHaveAnA = () => { - // +const sortNumbersWithNoArgument = (nums) => { + return nums.sort(num => num) + +} + +const sortWordsWithNoArgument = (words) => { + return words.sort(word => word) + }; +const sortNumbersAscendingOrder = (nums) => { + return nums.sort((a, b)=> a - b) + +}; + +const sortNumbersDescendingOrder = (nums) => { + return nums.sort((a, b) => b - a) + +}; + +const sortWordsAscendingOrder = (words) => { + return words.sort((a, b) = a.localCompare(b)) + +}; + +const sortWordsDescendingOrder = (words) => { + return words.sort((a, b) => b.localCompare(a)) + +}; + + module.exports = { isEveryNumGreaterThan2, isEveryWordShorterThan7,