Reputation: 147
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.
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 voterResults(arr) {
// your code here
}
console.log(voterResults(voters)); // Returned value shown below:
/*
{ youngVotes: 1,
youth: 4,
midVotes: 3,
mids: 4,
oldVotes: 3,
olds: 4
}
I am being trying out this specific problem, below is what I have tried, where i am able to form the hashmap. But not sure how I can solve the above problem.
function voterResults(arr) {
let votesArray = ['youngVotes', 'youth', 'midVotes', 'mids',
'oldVotes', 'olds']
return votesArray.reduce((acc, it) => {
acc[it] = (acc[it] || 0) + 1
return acc;
}, {})
}
//output
{
youngVotes: 1 ,
youth: 1 ,
midVotes: 1 ,
mids: 1 ,
oldVotes: 1 ,
olds: 1
}
Actual output needed:
{
youngVotes: 1,
youth: 4,
midVotes: 3,
mids: 4,
oldVotes: 3,
olds: 4
}
Upvotes: 0
Views: 824
Reputation: 1
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}
];
const age = (age) => {
return age<26? ['numYoungPeople', 'numYoungVotes']:
(age < 35 ? ['numMidsPeople', 'numMidVotesPeople']:
['numOldsPeople', 'numOldVotesPeople'])
}
function voterResults(arr) {
const obs = arr.reduce((acc, num) =>{
acc[age(num.age)[0]] =(acc[age(num.age)[0]] || 0) +1
acc[age(num.age)[1]] = num.voted? (acc[age(num.age)[1]] || 0 )+1: (acc[age(num.age)[1]] || 0)
return acc;
}, {})
return obs;
}
console.log(voterResults(voters)); // Returned value shown below:
/*
{ numYoungVotes: 1,
numYoungPeople: 4,
numMidVotesPeople: 3,
numMidsPeople: 4,
numOldVotesPeople: 3,
numOldsPeople: 4
}
*/
Upvotes: 0
Reputation: 11001
Here is using reduce
method and age groups can be extended easily.
function voterResults(arr) {
const ranges = { youngVotes: [18, 25], youth: [26, 35] };
return arr
.filter(x => x.voted)
.reduce((acc, curr) => {
Object.keys(ranges).forEach(key => {
if (curr.age >= ranges[key][0] && curr.age <= ranges[key][1]) {
acc[key] = key in acc ? acc[key] + 1 : 1;
}
});
return acc;
}, {});
}
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 }
];
console.log(voterResults(voters));
Upvotes: 1
Reputation: 193
You can do simply using if conditions
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 voterResults(){
let youngVotes = 0;
let youth = 0;
let midVotes = 0;
let mids =0;
let oldVotes =0;
let olds =0;
for (var i = voters.length - 1; i >= 0; i--) {
if(voters[i].age >= 18 && voters[i].age <= 25 && voters[i].voted === true){
youngVotes++;
}
if(voters[i].age >= 18 && voters[i].age <= 25){
youth++;
}
if(voters[i].age >= 26 && voters[i].age <= 35 && voters[i].voted === true){
midVotes++;
}
if(voters[i].age >= 26 && voters[i].age <= 35 ){
mids++;
}
if(voters[i].age >= 36 && voters[i].age <= 55 && voters[i].voted === true){
oldVotes++;
}
if(voters[i].age >= 36 && voters[i].age <= 55){
olds++;
}
}
return{
youngVotes,
youth,
midVotes,
mids,
oldVotes,
olds
}
}
console.log(voterResults())
Upvotes: 0
Reputation: 1023
You need to use input arr, and use-value of voted and use age to classify and increment value in the object.
const 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}
];
// define your limit here, will check for <= of defined age
let categories = { youngVotes: 21, youth: 30, midVotes: 40, mids: 50, oldVotes: 60, olds: 130}
function voterResults(arr) {
const conditions = Object.entries(categories);
return arr.reduce((val, vote)=>{
if(vote.voted) {
for(let i=0;i<conditions.length;i++) {
if(vote.age <= conditions[i][1]) {
val[conditions[i][0]] = val[conditions[i][0]] ? val[conditions[i][0]] + 1 : 1;
return val;
}
}
}
return val;
}, {})
}
console.log(voterResults(voters));
Upvotes: 1
Reputation: 370659
I'd first make a helper function that returns the property strings corresponding to the age passed (eg 20
-> ['youth', 'youngVotes']
). Then use .reduce
to iterate over the voters
array - call that function to find out which property to increment, and increment it:
const getCat = (age) => {
if (age < 25) return ['youth', 'youngVotes'];
if (age < 35) return ['mids', 'midVotes'];
return ['olds', 'oldVotes'];
};
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}
];
const counts = voters.reduce((a, { age, voted }) => {
const [prop, voteProp] = getCat(age);
a[prop] = (a[prop] || 0) + 1;
if (voted) {
a[voteProp] = (a[voteProp] || 0) + 1;
}
return a;
}, {});
console.log(counts);
Upvotes: 6