Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array Method Drill Lab Completed #73

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 49 additions & 34 deletions _tests_/array-methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
});
});
35 changes: 35 additions & 0 deletions drills-more.js
Original file line number Diff line number Diff line change
@@ -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,
};
89 changes: 52 additions & 37 deletions solution.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -84,4 +97,6 @@ module.exports = {
arrayWordsUpcased,
areSomeNumsDivisibleBy7,
doSomeWordsHaveAnA,
sumOfNums,
wordsToSentence,
};