How to put an array as a function parameter in js?

Asked

Viewed 1,567 times

-1

I need to write a function that takes two parameters, the first being an array of numbers and the second being any number.

The function needs to check whether the second parameter (which can be any number) is contained in the array (first parameter)

I thought about the following javascript code:

function contem(array,y){

  var array = []; 

### tentei fazer o programa ler o primeiro parâmetro como um array(ñ funcionou)

  for(i = 0; i < array.lenght; i++){

    if (i == y){

      return true
    }

    else {

      return false 
    }
  }
}

explanation of the code / doubts:

  • How do for q the function understands that the first parameter is an array?

  • I thought to declare a variable and say that it is equal to an empty array, but it is not working.

    • My idea is to create a loop for q scans the array and if y (second parameter) returns true, otherwise (Else) returns false.

BS.: If you have something you can read too eh valid, this exercise is part of an online challenge, I am about 10 hours trying to do and nda ....

2 answers

1

Some points:

  • The property is called length, nay lenght.
  • You are comparing the index value instead of the content value at a given position, i == y is different from array[i] == y (if possible, use === instead of ==, the difference between these operators can be seen here).
  • In the first iteration (when the index is still 0), its function is already returning a boolean for any condition of the value to exist or not in the array. The false return should be outside the loop. Since, when iterating under the entire array and the value is not present, it should be returned false.
  • Your function has a name parameter array and then you declare a variable with the same name var array = [];.
  • Although it works in this case, you are not declaring the variable i in the loop: for(i = 0; i < array.lenght; i++). It is right to use var, let or const, according to your need.

// Editando pouca coisa no código da pergunta
function contem(array,y){
  for(var i = 0; i < array.length; i++){
    if (array[i] == y){
      return true;
    }
  }
  return false;
}

var array = [1, 2, 3, 4, 5]
console.log(contem(array, 2))
console.log(contem(array, 4))
console.log(contem(array, 6))

There are points that can still be improved, but I believe it is discussion for another issue, another time.

  • 1

    The correct is if(array[i] == y).

  • @v.Saints Thanks, corrected.

0

To know if a variable is an array, you can both use Operator instanceof or the function Array.isArray(). Check out:

var numero = 1;
var numeros = [5, 7, 42];

numeros instanceof Array;
Array.isArray(numeros);
//Em ambos os casos retorna true

numero instanceof Array;
Array.isArray(numero);
//Em ambos os casos retorna false

Now, in your specific case, do you need to make sure that the first argument is an array? Well, that definitely doesn’t seem necessary. Just do your job without that statement var array = []; and a few more fixes and things will work out. Check yourself:

function contem(array,y){

  for(i = 0; i < array.lenght; i++){

    if (array[i] == y) return true;
     //corrigi a condição no interior do if e retirei o else return false de dentro do loop.
    }
  return false; 
}

The curious thing is that javascript already has a function that checks whether an element is inside an array. This allows a shorter solution through a wrapper. See:

function contem(arr, y){
  return arr.includes(y);
}

And finally, I had the idea to solve the problem recursively as briefly as possible. I think you won’t understand ducks, even so, I leave the solution as a curiosity and for a little person in a stage a little more advanced. Cf:

function contem(arr, y, i=0){
  return arr[i] == y || (i < arr.length 
    && contem(arr, y, ++i));
}

var numeros = [5, 7, 42];

console.log(contem(numeros, 42));
console.log(contem(numeros, 28));

  • just missed to declare i as variable up there inside the for loop. thanks a lot for the help :)

Browser other questions tagged

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