var two = function(num2,target){
// let arr = []
// num2.forEach((element,index) => {
// console.log(element)
// if(element + num2[index + 1]== target){
// console.log(index,parseInt(index+1))
// arr = [index,parseInt(index+1)]
// // return [index,parseInt(index+1)]
// }
// });
// return arr
for(let item in num2){
console.log(parseInt(num2[item]) + parseInt(num2[parseInt(item + 1)]))
if(parseInt(num2[item]) + parseInt(num2[parseInt(item + 1)])== target){
console.log(item,parseInt(item+1))
return [item,parseInt(item+1)]
}
}
}
console.log(two([2,7,11,15],9))
上述注释部分效果 === 下面部门
结论: for in 获取的值为字符串格式 forEach为数字格式
for in return之后会结束循环 forEach会继续执行