diff --git a/_tests_/array-methods.test.js b/_tests_/array-methods.test.js index f675dc9..c76c9fe 100644 --- a/_tests_/array-methods.test.js +++ b/_tests_/array-methods.test.js @@ -13,103 +13,118 @@ const { arrayWordsUpcased, areSomeNumsDivisibleBy7, doSomeWordsHaveAnA, -} = require("../solution.js"); + sumOfNums, + wordsToSentence, +} = require('../solution.js'); // Don't match the test data from solution code // Students tend to skip passing in a parameter in their solutions -const { nums, words } = require("../data/data.js"); +const { nums, words } = require('../data/data.js'); -describe("Array Methods", () => { - test("Every number is greater than or equal to 2", () => { +describe('Array Methods', () => { + test('Every number is greater than or equal to 2', () => { expect(/\.every/.test(isEveryNumGreaterThan2.toString())).toBe(true); expect(isEveryNumGreaterThan2(nums)).toBe(false); }); - test("Every word is shorter than 7 characters", () => { + test('Every word is shorter than 7 characters', () => { expect(/\.every/.test(isEveryWordShorterThan7.toString())).toBe(true); expect(isEveryWordShorterThan7(words)).toBe(true); }); - test("A new array with all values less than 5", () => { + test('A new array with all values less than 5', () => { arrayLessThan5; expect(/\.filter/.test(arrayLessThan5.toString())).toBe(true); expect(arrayLessThan5(nums)).toStrictEqual([1, 2, 3, 4, 0]); }); - test("A new array with all words with odd lengths", () => { + test('A new array with all words with odd lengths', () => { expect(/\.filter/.test(arrayOddLengthWords.toString())).toBe(true); expect(arrayOddLengthWords(words)).toStrictEqual([ - "The", - "quick", - "brown", - "fox", - "jumps", - "the", - "dog", + 'The', + 'quick', + 'brown', + 'fox', + 'jumps', + 'the', + 'dog', ]); }); - test("Find first value that is divisible by 4", () => { + test('Find first value that is divisible by 4', () => { expect(/\.find/.test(firstValDivisibleBy4.toString())).toBe(true); expect(firstValDivisibleBy4(nums)).toBe(4); }); - test("Find first word longer than 4 characters", () => { + test('Find first word longer than 4 characters', () => { expect(/\.find/.test(firstWordLongerThan4Char.toString())).toBe(true); - expect(firstWordLongerThan4Char(words)).toBe("quick"); + expect(firstWordLongerThan4Char(words)).toBe('quick'); }); - test("Find the index of the first number that is divisible by 3", () => { + test('Find the index of the first number that is divisible by 3', () => { expect(/\.findIndex/.test(firstNumIndexDivisibleBy3.toString())).toBe(true); expect(firstNumIndexDivisibleBy3(nums)).toBe(2); }); - test("Find the index of the first word that is less than 2 characters long", () => { + test('Find the index of the first word that is less than 2 characters long', () => { expect(/\.findIndex/.test(firstWordIndexLessThan2Char.toString())).toBe( true ); expect(firstWordIndexLessThan2Char(words)).toBe(-1); }); - test("Console log the values times 3", () => { + test('Console log the values times 3', () => { expect(/\.forEach/.test(logValuesTimes3.toString(nums))).toBe(true); expect(logValuesTimes3(nums)).toBe(undefined); }); - test("Console log each word with an exclamation point", () => { + test('Console log each word with an exclamation point', () => { expect(/\.forEach/.test(logWordsWithExclamation.toString(words))).toBe( true ); expect(logWordsWithExclamation(words)).toBe(undefined); }); - test("A new array of values that are squared and then multiplied by their index number", () => { + test('A new array of values that are squared and then multiplied by their index number', () => { expect(/\.map/.test(arrayValuesSquaredTimesIndex.toString())).toBe(true); expect(arrayValuesSquaredTimesIndex(nums)).toStrictEqual([ 0, 4, 18, 48, 100, 180, 294, 448, 648, 900, 0, ]); }); - test("A new array of words that are all uppercase", () => { + test('A new array of words that are all uppercase', () => { expect(/\.map/.test(arrayWordsUpcased.toString())).toBe(true); expect(arrayWordsUpcased(words)).toStrictEqual([ - "THE", - "QUICK", - "BROWN", - "FOX", - "JUMPS", - "OVER", - "THE", - "LAZY", - "DOG", + 'THE', + 'QUICK', + 'BROWN', + 'FOX', + 'JUMPS', + 'OVER', + 'THE', + 'LAZY', + 'DOG', ]); }); - test("Are some numbers divisible by 7?", () => { + test('Are some numbers divisible by 7?', () => { expect(/\.some/.test(areSomeNumsDivisibleBy7.toString())).toBe(true); expect(areSomeNumsDivisibleBy7(nums)).toBe(true); }); - test("Do some words have an a?", () => { + + test('Do some words have an a?', () => { expect(/\.some/.test(doSomeWordsHaveAnA.toString())).toBe(true); expect(doSomeWordsHaveAnA(words)).toBe(true); }); + + test('Is the sum of all numbers equal to 55?', () => { + expect(/\.reduce/.test(sumOfNums.toString())).toBe(true); + expect(sumOfNums(nums)).toBe(55); + }); + + test('Does the sentence from the words array make sense?', () => { + expect(/\.reduce/.test(wordsToSentence.toString())).toBe(true); + expect(wordsToSentence(words)).toBe( + 'The quick brown fox jumps over the lazy dog' + ); + }); }); diff --git a/drills-more.js b/drills-more.js new file mode 100644 index 0000000..98761c1 --- /dev/null +++ b/drills-more.js @@ -0,0 +1,35 @@ +const { nums, words } = require('./data/data.js'); + +//Sorting in descending order: +const descendingSort = (nums) => { + return nums.sort((a, b) => b - a); +}; +console.log('Descending from largest to smallest:', descendingSort(nums)); + +//Sorting in ascending order: +const ascendingSort = (nums) => { + return nums.sort((a, b) => a - b); +}; +console.log('Ascending from smallest to largest', ascendingSort(nums)); + +//Sorting in descending order: +const descendingWords = (words) => { + return words.map((word) => word.toLowerCase()).sort((a, b) => b - a); +}; +console.log( + 'Sorting by reverse alphabetical order (Z-A):', + descendingWords(words) +); + +//Sorting in ascending order: +const ascendingWords = (words) => { + return words.map((word) => word.toLowerCase()).sort(); +}; +console.log('Sorting by alphabetical order (A-Z):', ascendingWords(words)); + +module.exports = { + descendingSort, + ascendingSort, + descendingWords, + ascendingSort, +}; diff --git a/solution.js b/solution.js index 049257f..e26ea5d 100644 --- a/solution.js +++ b/solution.js @@ -1,74 +1,87 @@ -const { nums, words } = require("./data/data.js"); +const { nums, words } = require('./data/data.js'); -// Every -const isEveryNumGreaterThan2 = () => { - // +// Every => Determine if every element in the arrays: +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 both of the arrays to return: +const arrayLessThan5 = (nums) => { + return nums.filter((num) => num < 5); }; -const arrayOddLengthWords = () => { - // +const arrayOddLengthWords = (words) => { + return words.filter((word) => word.length % 2 === 1); }; -// Find - -const firstValDivisibleBy4 = () => { - // +// Find the first value of the arrays: +const firstValDivisibleBy4 = (nums) => { + return nums.find((num) => num % 4 === 0); }; -const firstWordLongerThan4Char = () => { - // +const firstWordLongerThan4Char = (words) => { + return words.find((word) => word.length > 4); }; // Find Index - -const firstNumIndexDivisibleBy3 = () => { - // +const firstNumIndexDivisibleBy3 = (nums) => { + return nums.findIndex((num) => num % 3 === 0); }; +console.log( + 'The first number index divisible by 3 is: ', + firstNumIndexDivisibleBy3(nums) +); -const firstWordIndexLessThan2Char = () => { - // +const firstWordIndexLessThan2Char = (words) => { + return words.findIndex((word) => word.length < 2); }; // For Each - -const logValuesTimes3 = () => { - // +const logValuesTimes3 = (nums) => { + nums.forEach((num) => console.log(num * 3)); }; -const logWordsWithExclamation = () => { - // +const logWordsWithExclamation = (words) => { + words.forEach((word) => console.log(`${word}!`)); }; // Map - -const arrayValuesSquaredTimesIndex = () => { - // +const arrayValuesSquaredTimesIndex = (nums) => { + return nums.map((num, index) => num * num * index); }; -const arrayWordsUpcased = () => { - // +const arrayWordsUpcased = (words) => { + return words.map((word) => word.toUpperCase()); }; // Some +const areSomeNumsDivisibleBy7 = (nums) => { + return nums.some((num) => num % 7 === 0); +}; + +const doSomeWordsHaveAnA = (words) => { + return words.some((word) => word.includes('a')); +}; + +/***** This is the bonus part *****/ -const areSomeNumsDivisibleBy7 = () => { - // +// Using the reduce method to add all numbers in the nums array: +const sumOfNums = (nums) => { + return nums.reduce((sum, num) => sum + num, 0); }; +console.log('The sum of the nums array is:', sumOfNums(nums)); -const doSomeWordsHaveAnA = () => { - // +// Using the reduce method to concatenate all the words together: +const wordsToSentence = (words) => { + return words.reduce((prev, curr) => prev + ' ' + curr); }; +console.log('The full sentence is:', wordsToSentence(words)); +// Export all the functions module.exports = { isEveryNumGreaterThan2, isEveryWordShorterThan7, @@ -84,4 +97,6 @@ module.exports = { arrayWordsUpcased, areSomeNumsDivisibleBy7, doSomeWordsHaveAnA, + sumOfNums, + wordsToSentence, };