Skip to content

Instantly share code, notes, and snippets.

View Chuloo's full-sized avatar

William Chuloo

View GitHub Profile
@Chuloo
Chuloo / array-filter-method.js
Created January 27, 2019 23:04
Filter numbers greater than 5
// 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)
@Chuloo
Chuloo / array-filter-method.js
Created January 27, 2019 23:23
Filter numbers within a set range
// 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);
@Chuloo
Chuloo / array-filter-method.js
Created January 27, 2019 23:27
Fetch students who passed a test
// Create array of students
const students = [
{
name: 'Peter',
score: 50
},
{
name: 'Sarah',
score: 40
},
@Chuloo
Chuloo / array-filter-method.js
Created January 27, 2019 23:30
List items over an array index
// 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);
@Chuloo
Chuloo / array-join-method.js
Last active January 29, 2019 13:09
Join words in an array
// 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
@Chuloo
Chuloo / array-join-method.js
Created January 29, 2019 12:38
Concatenate elements in an array
// new array
const words = ['Hello', 'world!'];
// join elements in array
const greeting = words.join(' ');
// display new string
console.log(greeting);
@Chuloo
Chuloo / array-join-method.js
Created January 29, 2019 12:41
Remove all commas in a string
// 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
@Chuloo
Chuloo / array-find-method.js
Created January 29, 2019 13:48
Get item in array
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)
@Chuloo
Chuloo / array-every-method.js
Last active February 6, 2019 17:11
Check if each item in an array is less than 100
// Create new array
const array = [20, 21, 23, 15, 2]
// Create condition
const isLessThan100 = (number)=> number < 100
// Apply function
let result = array.every(isLessThan100)
// Output result
@Chuloo
Chuloo / array-every-method.js
Last active February 6, 2019 17:10
Test for users that can drive
// 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)