Sort an array from bottom to top and reverse. Get only the 10% of the total array
up vote
0
down vote
favorite
"missed_votes_pct" = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42]
function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);
if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
console.log(temp);
}
function bottomTenPercent() {
members.sort(function(a, b) {
return a.missed_votes_pct - b.missed_votes_pct; //sort our array from lower to higher and call getTenPerOfMissed(members)
//to keep the 10% of the lowest values
});
// console.log("Lowest" , members)
getTenPerOfMissed(members)
}
function TopTenPercent() {
members.sort(function(a, b) {
return b.missed_votes_pct - a.missed_votes_pct;
});
getTenPerOfMissed(members);
}
bottomTenPercent();
TopTenPercent();
I have create these three functions to sort an array from higher to lower and reverse. Also i need to keep only the 10% percent of the numbers and i have to keep the number that they are repeated.
When I print my temp array with the sorted numbers are unable to work together both of the sorted functions. If i call only one of them work perfect.
Also i need to have access in the temp because i want to print the numbers in a table. What process i have to follow because i can't realize at all the process and when i try to set the temp as an global variable my functions doesn't work well
Any ideas what i am doing wrong? because i have stuck on this task for two days. I am new in javascript so forgive me if my the question is dumb
javascript arrays object
|
show 6 more comments
up vote
0
down vote
favorite
"missed_votes_pct" = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42]
function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);
if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
console.log(temp);
}
function bottomTenPercent() {
members.sort(function(a, b) {
return a.missed_votes_pct - b.missed_votes_pct; //sort our array from lower to higher and call getTenPerOfMissed(members)
//to keep the 10% of the lowest values
});
// console.log("Lowest" , members)
getTenPerOfMissed(members)
}
function TopTenPercent() {
members.sort(function(a, b) {
return b.missed_votes_pct - a.missed_votes_pct;
});
getTenPerOfMissed(members);
}
bottomTenPercent();
TopTenPercent();
I have create these three functions to sort an array from higher to lower and reverse. Also i need to keep only the 10% percent of the numbers and i have to keep the number that they are repeated.
When I print my temp array with the sorted numbers are unable to work together both of the sorted functions. If i call only one of them work perfect.
Also i need to have access in the temp because i want to print the numbers in a table. What process i have to follow because i can't realize at all the process and when i try to set the temp as an global variable my functions doesn't work well
Any ideas what i am doing wrong? because i have stuck on this task for two days. I am new in javascript so forgive me if my the question is dumb
javascript arrays object
Hi there, to properly illustrate your question can you give us examples of the input, and what you expect the output to be?
– James
Nov 11 at 14:02
What do you mean by 10% ? If the input is[1, 2, 3, 4, 5, 6, 7, 8, 9]
whats the expected output?
– Jonas Wilms
Nov 11 at 14:07
can you share the array?
– brk
Nov 11 at 14:08
Thank you very much. The output should be array.length * 0.1 and if there are repeated number should me included it as well.
– Ioan Dimi
Nov 11 at 14:09
What should the output from those numbers be. Make it clear so you get a good answer.
– James
Nov 11 at 14:15
|
show 6 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
"missed_votes_pct" = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42]
function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);
if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
console.log(temp);
}
function bottomTenPercent() {
members.sort(function(a, b) {
return a.missed_votes_pct - b.missed_votes_pct; //sort our array from lower to higher and call getTenPerOfMissed(members)
//to keep the 10% of the lowest values
});
// console.log("Lowest" , members)
getTenPerOfMissed(members)
}
function TopTenPercent() {
members.sort(function(a, b) {
return b.missed_votes_pct - a.missed_votes_pct;
});
getTenPerOfMissed(members);
}
bottomTenPercent();
TopTenPercent();
I have create these three functions to sort an array from higher to lower and reverse. Also i need to keep only the 10% percent of the numbers and i have to keep the number that they are repeated.
When I print my temp array with the sorted numbers are unable to work together both of the sorted functions. If i call only one of them work perfect.
Also i need to have access in the temp because i want to print the numbers in a table. What process i have to follow because i can't realize at all the process and when i try to set the temp as an global variable my functions doesn't work well
Any ideas what i am doing wrong? because i have stuck on this task for two days. I am new in javascript so forgive me if my the question is dumb
javascript arrays object
"missed_votes_pct" = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42]
function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);
if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
console.log(temp);
}
function bottomTenPercent() {
members.sort(function(a, b) {
return a.missed_votes_pct - b.missed_votes_pct; //sort our array from lower to higher and call getTenPerOfMissed(members)
//to keep the 10% of the lowest values
});
// console.log("Lowest" , members)
getTenPerOfMissed(members)
}
function TopTenPercent() {
members.sort(function(a, b) {
return b.missed_votes_pct - a.missed_votes_pct;
});
getTenPerOfMissed(members);
}
bottomTenPercent();
TopTenPercent();
I have create these three functions to sort an array from higher to lower and reverse. Also i need to keep only the 10% percent of the numbers and i have to keep the number that they are repeated.
When I print my temp array with the sorted numbers are unable to work together both of the sorted functions. If i call only one of them work perfect.
Also i need to have access in the temp because i want to print the numbers in a table. What process i have to follow because i can't realize at all the process and when i try to set the temp as an global variable my functions doesn't work well
Any ideas what i am doing wrong? because i have stuck on this task for two days. I am new in javascript so forgive me if my the question is dumb
"missed_votes_pct" = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42]
function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);
if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
console.log(temp);
}
function bottomTenPercent() {
members.sort(function(a, b) {
return a.missed_votes_pct - b.missed_votes_pct; //sort our array from lower to higher and call getTenPerOfMissed(members)
//to keep the 10% of the lowest values
});
// console.log("Lowest" , members)
getTenPerOfMissed(members)
}
function TopTenPercent() {
members.sort(function(a, b) {
return b.missed_votes_pct - a.missed_votes_pct;
});
getTenPerOfMissed(members);
}
bottomTenPercent();
TopTenPercent();
"missed_votes_pct" = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42]
function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);
if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
console.log(temp);
}
function bottomTenPercent() {
members.sort(function(a, b) {
return a.missed_votes_pct - b.missed_votes_pct; //sort our array from lower to higher and call getTenPerOfMissed(members)
//to keep the 10% of the lowest values
});
// console.log("Lowest" , members)
getTenPerOfMissed(members)
}
function TopTenPercent() {
members.sort(function(a, b) {
return b.missed_votes_pct - a.missed_votes_pct;
});
getTenPerOfMissed(members);
}
bottomTenPercent();
TopTenPercent();
javascript arrays object
javascript arrays object
edited Nov 11 at 14:14
asked Nov 11 at 13:59
Ioan Dimi
235
235
Hi there, to properly illustrate your question can you give us examples of the input, and what you expect the output to be?
– James
Nov 11 at 14:02
What do you mean by 10% ? If the input is[1, 2, 3, 4, 5, 6, 7, 8, 9]
whats the expected output?
– Jonas Wilms
Nov 11 at 14:07
can you share the array?
– brk
Nov 11 at 14:08
Thank you very much. The output should be array.length * 0.1 and if there are repeated number should me included it as well.
– Ioan Dimi
Nov 11 at 14:09
What should the output from those numbers be. Make it clear so you get a good answer.
– James
Nov 11 at 14:15
|
show 6 more comments
Hi there, to properly illustrate your question can you give us examples of the input, and what you expect the output to be?
– James
Nov 11 at 14:02
What do you mean by 10% ? If the input is[1, 2, 3, 4, 5, 6, 7, 8, 9]
whats the expected output?
– Jonas Wilms
Nov 11 at 14:07
can you share the array?
– brk
Nov 11 at 14:08
Thank you very much. The output should be array.length * 0.1 and if there are repeated number should me included it as well.
– Ioan Dimi
Nov 11 at 14:09
What should the output from those numbers be. Make it clear so you get a good answer.
– James
Nov 11 at 14:15
Hi there, to properly illustrate your question can you give us examples of the input, and what you expect the output to be?
– James
Nov 11 at 14:02
Hi there, to properly illustrate your question can you give us examples of the input, and what you expect the output to be?
– James
Nov 11 at 14:02
What do you mean by 10% ? If the input is
[1, 2, 3, 4, 5, 6, 7, 8, 9]
whats the expected output?– Jonas Wilms
Nov 11 at 14:07
What do you mean by 10% ? If the input is
[1, 2, 3, 4, 5, 6, 7, 8, 9]
whats the expected output?– Jonas Wilms
Nov 11 at 14:07
can you share the array?
– brk
Nov 11 at 14:08
can you share the array?
– brk
Nov 11 at 14:08
Thank you very much. The output should be array.length * 0.1 and if there are repeated number should me included it as well.
– Ioan Dimi
Nov 11 at 14:09
Thank you very much. The output should be array.length * 0.1 and if there are repeated number should me included it as well.
– Ioan Dimi
Nov 11 at 14:09
What should the output from those numbers be. Make it clear so you get a good answer.
– James
Nov 11 at 14:15
What should the output from those numbers be. Make it clear so you get a good answer.
– James
Nov 11 at 14:15
|
show 6 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
The easiest way to open up that temp array is to return it from your getTenPerOfMissed function. Then, whatever function called it has access to the modified array.
function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);
if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
return temp;
}
var topten = getTenPerOfMissed(array);
// topten is the "temp" array from getTenPerOfMissed
// sort array differently
array.sort(...);
var bottomten = getTenPerOfMissed(array);
So now you have define each array in a new variable?
– Ioan Dimi
Nov 11 at 14:40
Yeah, and then you work with the topten and bottomten variables, writing them out to your display or whatever you're going to do with them.
– James
Nov 11 at 15:03
Really thanks, it's so easy, but my mind did not work ον this way I need a lot of practice ....
– Ioan Dimi
Nov 11 at 15:07
add a comment |
up vote
0
down vote
When you are sorting , you dont need this a.missed_votes_pct - b.missed_votes_pct
, this expect missed_votes_pct
to be an key inside an object.In that case the array will look like this
[{missed_votes_pct:someVal},{missed_votes_pct:someVal}]
Only sorting missed_votes_pct
which is just an array will be fine. You can either use Math.ceil
or Math.floor
to get the round digit because 10% array length can also give a floating number.
You can use splice
to create a sub array.
Technically you dont need two function to get top and bottom 10%.You can sort in ascending order & use array.splice(start,howmany)
to get top 10 & array.splice(-10)
to get last 10 which highest
let missed_votes_pct = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42
];
function getTenPerOfMissed(array) {
var temp = ;
var len = Math.ceil(array.length * 0.1);
return array.splice(0, 10);
}
function bottomTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return a - b;
});
return getTenPerOfMissed(memsortedMems)
}
function TopTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return b - a;
});
return getTenPerOfMissed(memsortedMems);
}
console.log(bottomTenPercent(missed_votes_pct));
console.log(TopTenPercent(missed_votes_pct));
Thank everyone for your support I solve it . I can't thank you enough guys!!!
– Ioan Dimi
Nov 11 at 15:12
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The easiest way to open up that temp array is to return it from your getTenPerOfMissed function. Then, whatever function called it has access to the modified array.
function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);
if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
return temp;
}
var topten = getTenPerOfMissed(array);
// topten is the "temp" array from getTenPerOfMissed
// sort array differently
array.sort(...);
var bottomten = getTenPerOfMissed(array);
So now you have define each array in a new variable?
– Ioan Dimi
Nov 11 at 14:40
Yeah, and then you work with the topten and bottomten variables, writing them out to your display or whatever you're going to do with them.
– James
Nov 11 at 15:03
Really thanks, it's so easy, but my mind did not work ον this way I need a lot of practice ....
– Ioan Dimi
Nov 11 at 15:07
add a comment |
up vote
1
down vote
accepted
The easiest way to open up that temp array is to return it from your getTenPerOfMissed function. Then, whatever function called it has access to the modified array.
function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);
if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
return temp;
}
var topten = getTenPerOfMissed(array);
// topten is the "temp" array from getTenPerOfMissed
// sort array differently
array.sort(...);
var bottomten = getTenPerOfMissed(array);
So now you have define each array in a new variable?
– Ioan Dimi
Nov 11 at 14:40
Yeah, and then you work with the topten and bottomten variables, writing them out to your display or whatever you're going to do with them.
– James
Nov 11 at 15:03
Really thanks, it's so easy, but my mind did not work ον this way I need a lot of practice ....
– Ioan Dimi
Nov 11 at 15:07
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The easiest way to open up that temp array is to return it from your getTenPerOfMissed function. Then, whatever function called it has access to the modified array.
function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);
if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
return temp;
}
var topten = getTenPerOfMissed(array);
// topten is the "temp" array from getTenPerOfMissed
// sort array differently
array.sort(...);
var bottomten = getTenPerOfMissed(array);
The easiest way to open up that temp array is to return it from your getTenPerOfMissed function. Then, whatever function called it has access to the modified array.
function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);
if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
return temp;
}
var topten = getTenPerOfMissed(array);
// topten is the "temp" array from getTenPerOfMissed
// sort array differently
array.sort(...);
var bottomten = getTenPerOfMissed(array);
answered Nov 11 at 14:37
James
11.6k21430
11.6k21430
So now you have define each array in a new variable?
– Ioan Dimi
Nov 11 at 14:40
Yeah, and then you work with the topten and bottomten variables, writing them out to your display or whatever you're going to do with them.
– James
Nov 11 at 15:03
Really thanks, it's so easy, but my mind did not work ον this way I need a lot of practice ....
– Ioan Dimi
Nov 11 at 15:07
add a comment |
So now you have define each array in a new variable?
– Ioan Dimi
Nov 11 at 14:40
Yeah, and then you work with the topten and bottomten variables, writing them out to your display or whatever you're going to do with them.
– James
Nov 11 at 15:03
Really thanks, it's so easy, but my mind did not work ον this way I need a lot of practice ....
– Ioan Dimi
Nov 11 at 15:07
So now you have define each array in a new variable?
– Ioan Dimi
Nov 11 at 14:40
So now you have define each array in a new variable?
– Ioan Dimi
Nov 11 at 14:40
Yeah, and then you work with the topten and bottomten variables, writing them out to your display or whatever you're going to do with them.
– James
Nov 11 at 15:03
Yeah, and then you work with the topten and bottomten variables, writing them out to your display or whatever you're going to do with them.
– James
Nov 11 at 15:03
Really thanks, it's so easy, but my mind did not work ον this way I need a lot of practice ....
– Ioan Dimi
Nov 11 at 15:07
Really thanks, it's so easy, but my mind did not work ον this way I need a lot of practice ....
– Ioan Dimi
Nov 11 at 15:07
add a comment |
up vote
0
down vote
When you are sorting , you dont need this a.missed_votes_pct - b.missed_votes_pct
, this expect missed_votes_pct
to be an key inside an object.In that case the array will look like this
[{missed_votes_pct:someVal},{missed_votes_pct:someVal}]
Only sorting missed_votes_pct
which is just an array will be fine. You can either use Math.ceil
or Math.floor
to get the round digit because 10% array length can also give a floating number.
You can use splice
to create a sub array.
Technically you dont need two function to get top and bottom 10%.You can sort in ascending order & use array.splice(start,howmany)
to get top 10 & array.splice(-10)
to get last 10 which highest
let missed_votes_pct = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42
];
function getTenPerOfMissed(array) {
var temp = ;
var len = Math.ceil(array.length * 0.1);
return array.splice(0, 10);
}
function bottomTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return a - b;
});
return getTenPerOfMissed(memsortedMems)
}
function TopTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return b - a;
});
return getTenPerOfMissed(memsortedMems);
}
console.log(bottomTenPercent(missed_votes_pct));
console.log(TopTenPercent(missed_votes_pct));
Thank everyone for your support I solve it . I can't thank you enough guys!!!
– Ioan Dimi
Nov 11 at 15:12
add a comment |
up vote
0
down vote
When you are sorting , you dont need this a.missed_votes_pct - b.missed_votes_pct
, this expect missed_votes_pct
to be an key inside an object.In that case the array will look like this
[{missed_votes_pct:someVal},{missed_votes_pct:someVal}]
Only sorting missed_votes_pct
which is just an array will be fine. You can either use Math.ceil
or Math.floor
to get the round digit because 10% array length can also give a floating number.
You can use splice
to create a sub array.
Technically you dont need two function to get top and bottom 10%.You can sort in ascending order & use array.splice(start,howmany)
to get top 10 & array.splice(-10)
to get last 10 which highest
let missed_votes_pct = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42
];
function getTenPerOfMissed(array) {
var temp = ;
var len = Math.ceil(array.length * 0.1);
return array.splice(0, 10);
}
function bottomTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return a - b;
});
return getTenPerOfMissed(memsortedMems)
}
function TopTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return b - a;
});
return getTenPerOfMissed(memsortedMems);
}
console.log(bottomTenPercent(missed_votes_pct));
console.log(TopTenPercent(missed_votes_pct));
Thank everyone for your support I solve it . I can't thank you enough guys!!!
– Ioan Dimi
Nov 11 at 15:12
add a comment |
up vote
0
down vote
up vote
0
down vote
When you are sorting , you dont need this a.missed_votes_pct - b.missed_votes_pct
, this expect missed_votes_pct
to be an key inside an object.In that case the array will look like this
[{missed_votes_pct:someVal},{missed_votes_pct:someVal}]
Only sorting missed_votes_pct
which is just an array will be fine. You can either use Math.ceil
or Math.floor
to get the round digit because 10% array length can also give a floating number.
You can use splice
to create a sub array.
Technically you dont need two function to get top and bottom 10%.You can sort in ascending order & use array.splice(start,howmany)
to get top 10 & array.splice(-10)
to get last 10 which highest
let missed_votes_pct = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42
];
function getTenPerOfMissed(array) {
var temp = ;
var len = Math.ceil(array.length * 0.1);
return array.splice(0, 10);
}
function bottomTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return a - b;
});
return getTenPerOfMissed(memsortedMems)
}
function TopTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return b - a;
});
return getTenPerOfMissed(memsortedMems);
}
console.log(bottomTenPercent(missed_votes_pct));
console.log(TopTenPercent(missed_votes_pct));
When you are sorting , you dont need this a.missed_votes_pct - b.missed_votes_pct
, this expect missed_votes_pct
to be an key inside an object.In that case the array will look like this
[{missed_votes_pct:someVal},{missed_votes_pct:someVal}]
Only sorting missed_votes_pct
which is just an array will be fine. You can either use Math.ceil
or Math.floor
to get the round digit because 10% array length can also give a floating number.
You can use splice
to create a sub array.
Technically you dont need two function to get top and bottom 10%.You can sort in ascending order & use array.splice(start,howmany)
to get top 10 & array.splice(-10)
to get last 10 which highest
let missed_votes_pct = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42
];
function getTenPerOfMissed(array) {
var temp = ;
var len = Math.ceil(array.length * 0.1);
return array.splice(0, 10);
}
function bottomTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return a - b;
});
return getTenPerOfMissed(memsortedMems)
}
function TopTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return b - a;
});
return getTenPerOfMissed(memsortedMems);
}
console.log(bottomTenPercent(missed_votes_pct));
console.log(TopTenPercent(missed_votes_pct));
let missed_votes_pct = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42
];
function getTenPerOfMissed(array) {
var temp = ;
var len = Math.ceil(array.length * 0.1);
return array.splice(0, 10);
}
function bottomTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return a - b;
});
return getTenPerOfMissed(memsortedMems)
}
function TopTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return b - a;
});
return getTenPerOfMissed(memsortedMems);
}
console.log(bottomTenPercent(missed_votes_pct));
console.log(TopTenPercent(missed_votes_pct));
let missed_votes_pct = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42
];
function getTenPerOfMissed(array) {
var temp = ;
var len = Math.ceil(array.length * 0.1);
return array.splice(0, 10);
}
function bottomTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return a - b;
});
return getTenPerOfMissed(memsortedMems)
}
function TopTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return b - a;
});
return getTenPerOfMissed(memsortedMems);
}
console.log(bottomTenPercent(missed_votes_pct));
console.log(TopTenPercent(missed_votes_pct));
edited Nov 11 at 15:05
answered Nov 11 at 14:50
brk
25.1k31939
25.1k31939
Thank everyone for your support I solve it . I can't thank you enough guys!!!
– Ioan Dimi
Nov 11 at 15:12
add a comment |
Thank everyone for your support I solve it . I can't thank you enough guys!!!
– Ioan Dimi
Nov 11 at 15:12
Thank everyone for your support I solve it . I can't thank you enough guys!!!
– Ioan Dimi
Nov 11 at 15:12
Thank everyone for your support I solve it . I can't thank you enough guys!!!
– Ioan Dimi
Nov 11 at 15:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249480%2fsort-an-array-from-bottom-to-top-and-reverse-get-only-the-10-of-the-total-arra%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hi there, to properly illustrate your question can you give us examples of the input, and what you expect the output to be?
– James
Nov 11 at 14:02
What do you mean by 10% ? If the input is
[1, 2, 3, 4, 5, 6, 7, 8, 9]
whats the expected output?– Jonas Wilms
Nov 11 at 14:07
can you share the array?
– brk
Nov 11 at 14:08
Thank you very much. The output should be array.length * 0.1 and if there are repeated number should me included it as well.
– Ioan Dimi
Nov 11 at 14:09
What should the output from those numbers be. Make it clear so you get a good answer.
– James
Nov 11 at 14:15