Filter array whose elements are between two values

Asked

Viewed 2,108 times

1

A virtual store allows its visitors to filter products by price. There is an array of product prices. A programmer has already created a function maisBaratosQue(valor, precos) that returns an array with prices of products cheaper than the value passed as parameter. Another programmer has already created a function maisCarosQue(valor, precos) that returns an array with prices more expensive than the value passed as parameter. It’s your turn!

Create a function precosEntre(valorMenor, valorMaior, precos) that must use the functions maisBaratosQue and maisCarosQue to return the prices that are between the valorMenor and the valorMaior. Your function should then receive 3 parameters:

  • valorMenor to represent the minimum value of the prices to be listed
  • valorMaior to represent the maximum value of the prices to be listed
  • precos to represent an array of product prices

It should return an array with all prices between valorMenor and valorMaior.

Follow the code, I’m trying but I’m not getting:

function maisBaratosQue(valor, precos) {
   return precos.filter(p => p <= valor);
}
 
function maisCarosQue(valor, precos){
   return precos.filter(p => p >= valor);
}

function precosEntre(valorMenor, valorMaior, precos){
   return precos.filter(p => p <= valorMenor && p >= valorMaior);
}
  • 1

    What error is appearing? I tested here and the three functions seem to work well

  • 1

    Errors: When performing its function by passing "prices Center(5, 10, [ 1,2,3,4,5,6,7,8,9,10])" should return the following array: [5, 6, 7, 8, 9, 10]. Check the return.

  • 1

    James, have you ever done the tour? https://answall.com/tour Your question is very broad and doesn’t really have a problem. You can probably solve this problem by reading a bit of Javascript’s Map, Reduce and Filter documentation.

  • 1

    I’m new to javascript. Thanks

1 answer

3


The logic of your function is wrong. You did:

p <= valorMenor && p >= valorMaior

Supposing valorMenor be 5 and valorMaior be 10, what happens if p for 8? The above condition checks if 8 <= 5 (is not) and 8 >= 10 (is also not). And if you think about it, no number is at the same time less than 5 and greater than 10, then the filter will never find anything and will always return an empty array.

So actually what you should check is if p is among valorMenor and valorMaior, that is to say:

function precosEntre(valorMenor, valorMaior, precos) {
    // p deve estar entre valorMenor e valorMaior
    return precos.filter(p => valorMenor <= p && p <= valorMaior);
}

console.log(precosEntre(5, 10, [1,2,3,4,5,6,7,8,9,10])); // [ 5, 6, 7, 8, 9, 10 ]


Although the exercise "requires" the use of the functions maisBaratosQue and maisCarosQue. Therefore, you would first need to see everyone who is cheaper than valorMaior, and then check which ones are more expensive than valorMenor. Thus:

function precosEntre(valorMenor, valorMaior, precos) {
    var maisBaratos = maisBaratosQue(valorMaior, precos);
    return maisCarosQue(valorMenor, maisBaratos);
}

I mean, first I see the items that are cheaper than valorMaior. Of these, I see those who are more expensive than valorMenor. The result will be an array of all elements whose values are between valorMenor and valorMaior.

It can also be done at once without creating an intermediate variable:

function precosEntre(valorMenor, valorMaior, precos) {
    return maisCarosQue(valorMenor, maisBaratosQue(valorMaior, precos));
}

Full example:

function maisBaratosQue(valor, precos) {
    return precos.filter(p => p <= valor);
}
 
function maisCarosQue(valor, precos) {
    return precos.filter(p => p >= valor);
}

function precosEntre(valorMenor, valorMaior, precos) {
    return maisCarosQue(valorMenor, maisBaratosQue(valorMaior, precos));
}

console.log(precosEntre(5, 10, [1,2,3,4,5,6,7,8,9,10])); // [ 5, 6, 7, 8, 9, 10 ]


Just remember that this is not the best way, because we first create an array with the cheapest items, and then create another one with the most expensive ones (since filter always returns another array with the results). The first option above, doing everything at once, is better because it does not create an intermediate array unnecessarily (but as it was requirement of the exercise to use the functions maisCarosQue and maisBaratosQue, there’s not much to escape from).

  • 2

    That’s exactly what it is. If it weren’t for the demanding exercise I would do the first way, just changing the logic that was wrong. Thank you very much for your attention.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.