admin管理员组文章数量:1355694
How can I return the index position of "needle"
in this array?
function findNeedle(haystack) {
return findNeedle.indexOf('needle');
}
findNeedle(['3', '123124234', undefined, 'needle', 'world', 'hay', 2, '3', true, false]);
How can I return the index position of "needle"
in this array?
function findNeedle(haystack) {
return findNeedle.indexOf('needle');
}
findNeedle(['3', '123124234', undefined, 'needle', 'world', 'hay', 2, '3', true, false]);
Share
Improve this question
edited Jun 22, 2016 at 7:28
Quentin Roy
7,8972 gold badges34 silver badges52 bronze badges
asked Jun 22, 2016 at 7:20
Dimitris XydasDimitris Xydas
2332 gold badges5 silver badges9 bronze badges
3 Answers
Reset to default 3make it
return haystack.indexOf('needle');
you need to use the argument you have passed to the function instead of the function itself.
findNeedle
is the function, not the array that is passed as an argument of the function. Inside the function, haystack
is your array.
function findNeedle(haystack) {
return haystack.indexOf('needle');
}
var result = findNeedle(['3', '123124234', undefined, 'needle', 'world', 'hay', 2, '3', true, false]);
console.log(result);
You shoudn't run method .indexOf() on function. Call it on your property haystack
instead.
return haystack.indexOf('needle');
本文标签: javascriptIndexOf in a functionStack Overflow
版权声明:本文标题:javascript - IndexOf in a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743984930a2571236.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论