0
someone could tell me what I’m missing in this function of js?
alert("Insira o numero a ser pesquisado");
var num = prompt("Insira um numero: ");
var array = [2,23,4,56,7];
function pesquisa(array[], num){
var achou = false;
for(var i=0; i < array.length-1;i++){
if(array[i] == num)
{
achou = true;
}
}
return achou;
}
if(pesquisa(array,num) == true)
{
alert("o elemento procurado está dentro do array");
}
else
{
alert("O elemento não está no array");
}
Beware of the
i < array.length-1
, the<
already corrects the upper limit for the last element. In addition there is no notationarray[]
in javascript, switch toarray
only.– Wakim
I don’t understand why the boy’s question was denied....
– MarceloBoni
@Marcelobonifazio I wasn’t the downvoter, But I think the reason I voted against is because people tend to discourage questions like "find the mistake there for me" without any indication that something has been tried (like, which line is the mistake? gave error message or only the result that is wrong? etc).
– mgibsonbr