【发布时间】:2018-02-18 19:03:06
【问题描述】:
// Create a function named 'containsBool'that accepts an
// array as a parameter.
// You can remove the comments and use the following array:
// myArray = ['Wednesday',23,false];
// Create a for / in loop inside the function that iterates
// through the items in the array.
// In the loop, check each array item for 'typeof' data.
// If the array contains Boolean data, return true.
// Likewise, if the array does not contain Boolean data, return false.
// Call the function and log the returned Boolean to the console.
这是我的代码,不知道哪里不行:
var myArray = ['Wednesday', 23, true];
function containsBool(checkBool) {
for (g in checkBool) {
if (typeof checkBool[g] === 'boolean') {
return true;
} else {
return false;
}
}
}
console.log(containsBool(myArray));
【问题讨论】:
-
for...in不会遍历数组。使用for...of。 -
当你遇到第一个(任何)非布尔值时,你不能
return false,当你在整个数组中没有遇到时,你必须这样做 - 在循环之后。 -
@Bergi // 在函数内部创建一个 for / in 循环,该循环 // 遍历数组中的项目。 任务明确要求他使用
for...in。 -
@connexo 哦,好吧,我没读过,但它仍然是错误的。明确要求这一点的作业只能被视为对课程质量的一种指示……
标签: javascript arrays function