i have a problem, im trying to count how many child is age(as key) as below, but we want to get the bigkids group(ones that are the big kids in this case is 4 and 2) based on a kids >= 4 kids group i want to get the biggest number of age that can make a bigkids group.
question how can i get the age of the new created group with the biggest age under/same as 4 kids?
so heres what we get in input
max_bigest_kids_in_group = 4
kids object = {
//age 0 have 7 kids
'0' : 7
//age 1 have 3 kids
'1' : 3
//age 2 have 2 kids
'2' : 2
//age 4 have 1 kids
'4' : 1
//lets say there is no age 3 or 5,6 and so on.
}
heres what i have tried and currently made so far currently in psudo
try biggest next biggest
('4' = 1) + ('2' = 2) (oke becouse 1+2 is 3 >= 4) save array [4 and 2]
try biggest next biggest next biggest
('4' = 1) + ('2' = 2) + ('1' = 3) (not oke becouse 1+2+3 is 6 >= 4) stop becouse its more then x = 4
get array [4 and 2]
// expected result is {'4','2'} or ['4','2'] so if max_bigest_kids_in_group = 6 then it will be ['4','2','1']
how can we get that exptected result? in javascript
ps. sorry if the title is not that accurate, if there is any suggestion please comment or edit.
That's simple. Just make a loop over the kids, like in your pseudo code.
var kids = {
'0' : 7, //age 0 have 7 kids
'1' : 3, //age 1 have 3 kids
'2' : 2, //age 2 have 2 kids
'4' : 1 //age 4 have 1 kids
};
function getAgesOfOldestKids(n) {
/* get: {number} how many kids */
// lets begin with some magic to find out the maximum age in the set
// you might code this with a for-in-loop over kids
var max = Math.max.apply(null, Object.keys(kids));
var ages = []; // ages of the kids in the result
var count = 0; // how many kids are in the set
for (var i=max; i>=0; i--)
if (i in kids) { // age level exists
count += kids[i]; // add number of kids
if (count > n) // kids in the set are more than allowed
return ages; // break the loop
else
ages.push(i); // add this age step to the result
}
return ages; // there may be less children than requested
}
> getAgesOfOldestKids(4)
["4", "2"]
// a trick to get the number of kids in the result:
> var ages = [4, 2];
> ages.reduce(function(n, age){return n+kids[age]}, 0)
3
other possible and much shorter solution, looping directly over the kids' keys:
function getAgesOfOldestKids(n) {
/* get: {number} how many kids */
// ages, sorted descending
var ages = Object.keys(kids).sort(function(a,b){return b-a;});
var count = 0; // how many kids are in the set
for (var i=0; i<ages.length-1 && count <= n; i++)
count += kids[ages[i]]; // add number of kids
return ages.slice(0, i-1);
}
I assume your object kids
is not sorted by default. Also, in JS you can't rely on which order the for…in loop returns the properties (see the link for more details). So the first step is converting your object in a structure that you can sort and be sure the order you obtain is the correct one:
kids = {
//age 0 have 7 kids
'0' : 7,
//age 1 have 3 kids
'1' : 3,
//age 2 have 2 kids
'2' : 2,
//age 4 have 1 kids
'4' : 1
//lets say there is no age 3 or 5,6 and so on.
}
var array = [];
for (var age in kids) {
if (kids.hasOwnProperty(age)) {
array.push({age: age, count: kids[age]});
}
}
After you have it, you can sort the array as follow:
array.sort(function(a, b) { return a.count - b.count });
And then, you can makes your calculation:
var maxKids = 4;
var groups = [];
var count = 0;
for (var i = 0; i < array.length; i++) {
count += array[i].count;
if (count <= maxKids)
groups.push(array[i].age);
else
break;
}
console.log(groups);
This approach also covers when all groups of kids exceed the maximum number of kids you can have, so you will obtain an empty array.
Not sure I quite follow the problem, but, is it something along these lines?
http://jsfiddle.net/kyuff/Tkj2r/7/
var kids = {
//age 0 have 7 kids
0 : 7,
//age 1 have 3 kids
1 : 3,
//age 2 have 2 kids
2 : 2,
//age 4 have 1 kids
4 : 1
};
function between(min, max) {
var age = 0;
for(var age in kids) {
if( age >= min && age <= max) {
age += kids[age];
}
}
return age;
}
document.write( between(2,3) );