码迷,mamicode.com
首页 > 编程语言 > 详细

封装一个函数, 查看数字在数组中是否出现过, 如果出现过就返回数字在数组中的位置,没有出现过返回-1;

时间:2019-01-09 11:16:21      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:meta   function   lse   ret   返回   var   dex   turn   pre   

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
//封装一个函数, 查看数字在数组中是否出现过, 如果出现过就返回数字在数组中的位置,没有出现过返回-1;
//实例: console.log(indexOf(1, [1, 2, 3, 4, 5])) 返回结果: 0;
//console.log(indexOf(6, [1, 2, 3, 4, 5])) 返回结果: -1;
function indexOf(iNum, arr){
for(var i = 0; i < arr.length; i++){
if(iNum === arr[i]){
return i;
}else {
return -1
}
}
}

console.log( indexOf(1, [1, 2, 3, 4, 5]) );
console.log( indexOf(6, [1, 2, 3, 4, 5]) );

</script>
</head>
<body>

</body>
</html>

封装一个函数, 查看数字在数组中是否出现过, 如果出现过就返回数字在数组中的位置,没有出现过返回-1;

标签:meta   function   lse   ret   返回   var   dex   turn   pre   

原文地址:https://www.cnblogs.com/yxs1530/p/10242443.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!