Created
November 30, 2019 14:29
-
-
Save matheuswd/40f5d0e07b6d90fababf3b661d634c36 to your computer and use it in GitHub Desktop.
Reduce Exercise
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
/** 6) Given an array of potential voters, return an object representing the results of the vote | |
Include how many of the potential voters were in the ages 18-25, how many from 26-35, how many from 36-55, and how many of each of those age ranges actually voted. The resulting object containing this data should have 6 properties. See the example output at the bottom. | |
*/ | |
var voters = [ | |
{name:'Bob' , age: 30, voted: true}, | |
{name:'Jake' , age: 32, voted: true}, | |
{name:'Kate' , age: 25, voted: false}, | |
{name:'Sam' , age: 20, voted: false}, | |
{name:'Phil' , age: 21, voted: true}, | |
{name:'Ed' , age:55, voted:true}, | |
{name:'Tami' , age: 54, voted:true}, | |
{name: 'Mary', age: 31, voted: false}, | |
{name: 'Becky', age: 43, voted: false}, | |
{name: 'Joey', age: 41, voted: true}, | |
{name: 'Jeff', age: 30, voted: true}, | |
{name: 'Zack', age: 19, voted: false} | |
]; | |
function voterResultsFilters(arr) { | |
var youngVotesTrue = arr.filter(function(item) { | |
if (item.age >= 18 & item.age <= 25 && item.voted === true) { | |
return item; | |
} | |
}); | |
var young = arr.filter(function(item) { | |
if (item.age >= 18 & item.age <= 25) { | |
return item; | |
} | |
}); | |
var midVotesTrue = arr.filter(function(item) { | |
if (item.age >= 26 & item.age <= 35 && item.voted === true) { | |
return item; | |
} | |
}); | |
var mid = arr.filter(function(item) { | |
if (item.age >= 26 & item.age <= 35) { | |
return item; | |
} | |
}); | |
var oldVotesTrue = arr.filter(function(item) { | |
if (item.age >= 36 && item.voted === true) { | |
return item; | |
} | |
}); | |
var old = arr.filter(function(item) { | |
if (item.age >= 36) { | |
return item; | |
} | |
}); | |
return { | |
youngVotes: youngVotesTrue.length, | |
young: young.length, | |
midVotes: midVotesTrue.length, | |
mid: mid.length, | |
oldVotes: oldVotesTrue.length, | |
old: old.length, | |
}; | |
} | |
// console.log(voterResultsFilters(voters)); // Returned value shown below: | |
/* | |
{ youngVotes: 1, | |
youth: 4, | |
midVotes: 3, | |
mids: 4, | |
oldVotes: 3, | |
olds: 4 | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment