0

So I don’t want anyone to solve it but just help me out in terms of working a certain part. So out of the array I have to print a certain numbers that fit within the range given(0 & 50). Besides manually putting array[i]>= 0 && array[i] <=50

How can I make it so it test over multiple values and draws the numbers from there. I seem to be stuck on that part.

JJJ
  • 32,902
  • 20
  • 89
  • 102

3 Answers3

0

Edit (credits Guy Yogev):

function getRange(arr, min, max) {
  return arr.filter(function(entry) { return entry >= min && entry <= max; })
}

var testArr = [1, 2, 5, 10, 20, 30, 22, 4, 100];

console.log(getRange(testArr, 3, 10));
iacobalin
  • 523
  • 2
  • 9
0

The clean solution in java would be using streams. You can do much more than just filter, but this would solve your question.

int[] filterByRange(int[] array, int min, int max) {
    return Arrays.stream(array)
            .filter(i -> min <= i && i<= max)
            .toArray(int[]::new);
}
Adam Bates
  • 421
  • 4
  • 7
-4
array.slice(0,51) 

This is

array[i]>= 0 && array[i] < 51
Georgian Stan
  • 1,865
  • 3
  • 13
  • 27
  • Thanks but how do I make it to where it’s testing multiple times over different values and ranges. Like the user inputs the range and it’s not a set range? – Antonio Campos Mar 16 '19 at 07:41
  • array.slice takes out elements from an array, in your case the first 51, it doesn't check its value – baao Mar 16 '19 at 07:44
  • I don't think the question is about the index "0" and "50". It is more about to check if any element at x index, I mean A[x] is "more than numeric value 0 and less than value 50" or not – Kushagr Arora Mar 16 '19 at 07:54
  • @KushagrArora yes exactly! – Antonio Campos Mar 16 '19 at 08:54