I'm new in this of javascript and I'm wondering if there is a way to find more than one small value in a javascript array, lets say I'm given with an array of 100 values and they want me to find the 10% smaller numbers, and also another exercise with the same array but with the 10% bigger. Could you help me? thanks
Asked
Active
Viewed 117 times
-2
-
please add your data and your try. – Nina Scholz Oct 11 '19 at 08:22
-
_"excersise (exercise)"_? So this is a homework? – CodeRed Oct 11 '19 at 08:22
-
Welcome to stack overflow. Can you please have a look at this link: https://stackoverflow.com/help/how-to-ask PS: you can use filter but I leave it to you to google that. Please try to find the solution first before posting a question. Thanks. – GBouffard Oct 11 '19 at 08:23
-
Possible duplicate of [How to filter an array in javascript?](https://stackoverflow.com/questions/45916135/how-to-filter-an-array-in-javascript) – GBouffard Oct 11 '19 at 08:24
-
yes CodeRed is a homework.....but cant see its possible result – Enrique GF Oct 11 '19 at 09:14
-
Here's the hint might be useful for you. 1. Sort the array element 2. first 10 and last 10 element will be your smallest and biggest values. Alternatively, use implement bubble sort for K times , in your case k =10. I hope this will help you resolve your task with learning. – Vipul Patil Oct 11 '19 at 09:36
3 Answers
0
You could try something like this:
<script>
var numbers = [5, 6, 9, 12, 15, 39, 46, 78, 90, 98, 100];
var bottom_threshold = ((Math.max.apply(null, numbers)-Math.min.apply(null, numbers))*0.1)+Math.min.apply(null, numbers);
var top_threshold = (Math.max.apply(null, numbers)-Math.min.apply(null, numbers))*0.9;
for(let i = 0, max = numbers.length; i < max; i++) {
if(numbers[i] <= bottom_threshold) {
console.log("Bottom 10%: "+numbers[i]);
}
if(numbers[i] >= top_threshold) {
console.log("Top 10%: "+numbers[i]);
}
}
</script>

MiK
- 918
- 4
- 16
0
For this problem you need a very basic knowledge in Javascript, so as mentioned in the comments, it is is worth to give a try doing it by yourself, just Google around a little bit...
In case you really struggle:
Here you have a simple approach using the sort()
method, to sort the array, then filter()
the values by it's index:
// just a sample array with 100 random values
let values = [];
for (let i = 0; i < 100; i++) {
values.push(Math.random());
}
const filtered = values
.sort((a,b) => a-b)
.filter((_, index) => index >= 90)
console.log(filtered)
Obviously, this will be a lot nicer when wrapped in a function to make it more programmatically...

Shaya Ulman
- 1,299
- 1
- 12
- 24
0
I am wondering if you mean this:
function getAlmostLowestValuesByPercent(arr, percent) {
// catch erros here like check if arr isArray and percent is number
var itemsToSelect = Math.round(percent * arr.length / 100)
return arr.sort().slice(0, itemsToSelect)
}
So as an example:
getAlmostLowestValuesByPercent([1,3,4,2,0,9,5], 25) // => [0,1]

Ardeshir Valipoor
- 116
- 1
- 3
-
hey man ...really really thanks for the help...only 2 weeks since my first coding ever and already im given with this kind of apparently simple but bit complicated task....could you explain me a lil bit about this function , how did you reach to this conclusion in plain words, and why that expresion of if(arr.length==0)....really thanks – Enrique GF Oct 11 '19 at 09:59
-
No problem @EnriqueGF :) First of all arr.length==0 is not needed but we need to handle other exceptions as named in the comment. So about the code, first we need to understand _find the 10% smaller numbers_ and translate it to the code. It means that if we have n numbers we need one tenth of them, accordingly if we try to find p% of n numbers it equals to n*p/100 of the total. I also rounded to get more accurate results. Now we know that we need to select m items of total n numbers. To select we need to use slice. Finally when they're saying 10% smallest or biggest it implies sorting. – Ardeshir Valipoor Oct 11 '19 at 11:56