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

higher order array methods completed #122

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
100 changes: 75 additions & 25 deletions solution.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down