我正在尝试编写一个函数来检查数组的第一个或最后一个位置是否包含特定的数字。
那里的所有console.log
示例都应该输出true
。它的打印方式,我不认为我应该在我的函数中写入console.log
。我不能让他们都给true
看。
function checkForNumber(arr, num) {
if (arr[0] === num) {
//console.log(true) removed
return true
} else if (arr.slice(-1)[0] === num) {
//console.log(true) removed
return true
} else
return false;
}
console.log(checkForNumber([4, 2, 5, 3], 4) === true);
console.log(checkForNumber([4, 2, 5, 3], 3) === true);
console.log(checkForNumber([4, 2, 5, 3], 2) === false);
console.log(checkForNumber([4, 2, 5, 3], 13) === false);
https://stackoverflow.com/questions/52105459
复制相似问题