Even though you have the check inside the for
-loop, the code inside the for
-loop runs only once because now that you've added an else
, it will ALWAYS return something.
The correct way to return false if nothing was found, would be to add the return false
after the for-loop finishes.
for(var i = 0;i < anArray.length;i++)
{
if(num[i] == n)
{
return true;
}
}
return false;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…