This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We have an array with numbers | |
const numbers = [2, 4, 6, 8, 32, 100] | |
// Create function to filter any array | |
const functionToFilterNumbers = function(number){ | |
return number > 5; | |
} | |
// Filter the 'numbers' array | |
const filtered = numbers.filter(functionToFilterNumbers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create new array | |
const ages = [14, 12, 18, 21, 10]; | |
// filter array for numbers in a range | |
const canSeeDeadpool = ages.filter(function(age){ | |
return age >= 18; | |
}) | |
// display new filtered array | |
console.log(canSeeDeadpool); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create array of students | |
const students = [ | |
{ | |
name: 'Peter', | |
score: 50 | |
}, | |
{ | |
name: 'Sarah', | |
score: 40 | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create array of continents | |
const places = ['Africa', 'Asia', 'Europe', 'Australia']; | |
// Filter array for continents beyond the array index of 1 | |
const overIndex = places.filter(function(place, index){ | |
return index > 1; | |
}) | |
// Display new Array | |
console.log(overIndex); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// new array | |
const words = ['Hello', 'world!']; | |
// join elements in array | |
const greeting = words.join(' '); | |
// display new string | |
console.log(greeting); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// new string with commas | |
const stringWithCommas = 'I, want, to, come, home'; | |
// split string by the commas into an array | |
const wordArray = stringWithCommas.split(', ') | |
// join array into a string with a space as separator. | |
const stringWithoutCommas = wordArray.join(' '); | |
// display new string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create new array | |
const newArray = ['a', 'new', 'array'] | |
// join array with no spaces | |
const simpleString = newArray.join('') | |
// join array with dashes | |
const dashedString = newArray.join('-') | |
// display new strings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const items = ['nail', 'hammer', 'bolt']; | |
// find array item with index of 1 | |
const atIndex = items.find(function(element, index){ | |
return index === 1 | |
}) | |
// display array item found | |
console.log(atIndex) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create new array | |
const array = [2, 5, 10, 8, 20] | |
// Create test function | |
const testFunction = number => number % 5 === 0 | |
// Apply function | |
const result = array.some(testFunction) | |
// Display result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Age of users | |
const ages = [17, 18, 22, 25]; | |
// Age threshold to drive | |
const drivingAge = 16; | |
// Test users for who can drive | |
let canDrive = ages.every(function(age){return age >= drivingAge;}); | |
console.log(canDrive) |
OlderNewer