Return array with numbers larger than the reported Javascript

Asked

Viewed 500 times

-1

I need to return with a function all the numbers of a array larger than the one reported by the user. With the function I did here it is only returning the first number of the array, and I need to return the array complete.

function maiorNumero(array, numero)
{
   array = [70, 2, 9, 65, 5, -1, 0, 89, -5]
   numero= 7

  for(var i = 0; i < array.length; i++)
  {    
    if(array[i] > numero)
    {
      return array[i]
    }
  }
}

console.log(maiorNumero()) 

This function returns 70. The return I need is 70, 9, 65, 89.

  • Have you tried using Map, Reduce or Filter?

4 answers

3


You have to create a new array temporary to include items that meet the established condition. You cannot return an element only. So I was on the right track, but I had this problem. You keep adding the elements in the new array only if the element meets the defined criterion, and after passing through all returns the array. Think about it, if you want to return several elements that meet the criteria, the only way is to return one array, cannot return an element from a array.

There’s another problem that made the code weird. You have placed the values of the parameters inside the function, this must be arguments passed to the function.

I did it in a way close to what you did, I could have used a for of which would be more suitable for the case, but I kept the line you started to do. I could also make a filter ready, but I understood that the exercise tells you to do it by hand through a function of your own and not a filter. And it was like this:

function maiorNumero(array, numero) {
    var novo = [];
    for(var i = 0; i < array.length; i++) if (array[i] > numero) novo.push(array[i]);
    return novo;
}

console.log(maiorNumero([70, 2, 9, 65, 5, -1, 0, 89, -5], 7)) 

I put in the Github for future reference.

  • thank you very much, gave it right here and much simpler that was doing, vlw too

2

Javascript already brings with it some methods, and when it comes to array you can use the method filter, that will filter the array and return only the elements that check a certain condition, example:

var meuArray = [10, 50, 23, 70, 81, 90];
var arrayFiltrado = meuArray.filter(n => n > 70);

console.log(arrayFiltrado);

In this case you are filtering the meuArray and for each element of this array you check whether the element n is greater than the value you want and can also be a variable, in the example the arrayFiltrado will contain only elements of meuArray which are larger than 70.

1

You can use filter(). This creates a new array with all the elements that pass the test implemented by the provided function.

var array = [70, 2, 9, 65, 5, -1, 0, 89, -5];

    function numMaioresQue(arr, elem) {

      return arr.filter((item) => {
        return item > elem
      })
    }
    
console.log(numMaioresQue(array, 7))

0

var array = [70, 2, 9, 65, 5, -1, 0, 89, -5];
var elem = 7;
    function numMaioresQue(arr, elem) {

      return arr.filter((item) => {
        return item > elem
      })
    }
    
console.log(numMaioresQue(array, elem))

Browser other questions tagged

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