Created
July 17, 2018 18:56
-
-
Save rgaidot/8a01ef02b2d6c6ede6631e28975b2298 to your computer and use it in GitHub Desktop.
The basics of JavaScript with Arrays
This file contains hidden or 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
// Code Challenge #11: JavaScript Functional Programming | |
// https://scotch.io/bar-talk/code-challenge-11-javascript-functional-programming | |
// ARRAY 1 | |
const texasss = [{ | |
name: 'Mike', | |
age: 23, | |
gender: 'm', | |
us: false, | |
}, | |
{ | |
name: 'Liz', | |
age: 20, | |
gender: 'f', | |
us: true, | |
}, | |
{ | |
name: 'Chris', | |
age: 102, | |
gender: 'm', | |
us: true, | |
}, | |
{ | |
name: 'Chuloo', | |
age: 27, | |
gender: 'm', | |
us: false, | |
}, | |
{ | |
name: 'Annie', | |
age: 30, | |
gender: 'f', | |
us: true, | |
}, | |
] | |
// Part 1 - Find all users older than 24 | |
console.log('Array 1 - Part 1 - Find all users older than 24'); | |
console.table(texasss.filter(user => user.age > 24)); | |
// Part 2 - Find the total age of all users | |
const totalAgeReducer = (accumulator, currentUser) => accumulator + currentUser.age; | |
console.log(`Array 1 - Part 2 - Find the total age of all users: ${texasss.reduce(totalAgeReducer, 0)} total age`); | |
// Part 3 - List all female coders | |
console.log('Array 1 - Part 3 - List all female coders'); | |
console.table(texasss.filter(user => user.gender === 'f')); | |
// ARRAY 2 | |
const newieyork = [{ | |
name: 'Michelle', | |
age: 19, | |
coder: true, | |
gender: 'f', | |
us: true, | |
}, | |
{ | |
name: 'Sam', | |
age: 25, | |
coder: false, | |
gender: 'm', | |
us: false, | |
}, | |
{ | |
name: 'Ivy', | |
age: 26, | |
coder: true, | |
gender: 'f', | |
us: false, | |
}, | |
{ | |
name: 'Nick', | |
age: 32, | |
coder: true, | |
gender: 'm', | |
us: true, | |
}, | |
{ | |
name: 'Jim Beglin', | |
age: 65, | |
coder: false, | |
gender: 'm', | |
us: true, | |
}, | |
] | |
// Part 1 - List all users in US in ascending order | |
console.log('Array 2 - Part 1 - List all users in US in ascending order'); | |
console.table(newieyork.filter(user => user.us).sort((userPreview, userNext) => userPreview.name.localeCompare(userNext.name))); | |
// Part 2 - Sort all users by age | |
console.log('Array 2 - Part 2 - Sort all users by age'); | |
console.table(newieyork.sort((userPreview, userNext) => userPreview.age < userNext.age ? -1 : 1)); | |
// Part 3 - List all female coders | |
console.log('Array 2 - Part 3 - List all female coders'); | |
console.table(newieyork.filter(user => user.gender === 'f' && user.coder)); | |
// ARRAY 3 | |
const vegzas = [{ | |
name: 'Charly', | |
age: 32, | |
coder: true, | |
gender: 'm', | |
}, | |
{ | |
name: 'Law', | |
age: 21, | |
coder: true, | |
gender: 'm', | |
}, | |
{ | |
name: 'Rosey', | |
age: 42, | |
coder: false, | |
gender: 'f', | |
}, | |
{ | |
name: 'Steph', | |
age: 18, | |
coder: true, | |
gender: 'f' | |
}, | |
{ | |
name: 'Jon', | |
age: 47, | |
coder: false, | |
gender: 'm', | |
}, | |
] | |
// Part 1 - Find the total age of male coders under 25 | |
const totalAgeMaleReducer = (accumulator, currentUser) => accumulator + currentUser.age; | |
console.log(`Array 3 - Part 1 - Find the total age of male coders under 25: ${vegzas.filter(user => user.gender === 'm' & user.age < 25).reduce(totalAgeMaleReducer, 0)} total age`); | |
// Part 2 - List all male coders over 30 | |
console.log('Array 3 - Part 2 - List all male coders over 30'); | |
console.table(vegzas.filter(user => user.gender === 'm' && user.coder && user.age > 30)); | |
// Part 3 - Find the total age of everyone in texasss, newieyork and vegzas combined. | |
console.log(`Array 3 - Part 3 - Find the total age of everyone in texasss, newieyork and vegzas combined: ${[...texasss, ...newieyork, ...vegzas].reduce(totalAgeMaleReducer, 0)} total age`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment