【发布时间】:2019-10-17 19:41:05
【问题描述】:
两个数组都包含字符串“hello”,但只有第一个记录为真。为什么?
function containsHello(array){
for(let i = 0; i < array.length; i++){
if(array[i] === 'hello'){
return true;
} else {
return false;
}
}
}
console.log(containsHello(['hello', 'no', 'yes'])); // logs true
console.log(containsHello(['no', 'hello', 'yes'])); // logs false
【问题讨论】:
-
在循环中添加
console.log。打印多少次?您希望它打印多少次? -
试试 == 而不是 ===,看看会发生什么。
标签: javascript arrays