function and(arr) {
if (arr.length === 0) {
return true;
}
const head = arr[0];
const tail = arr.slice(1);
return head && and(tail);
}
and([true, true, false]);
Run Code Online (Sandbox Code Playgroud)
我有这个递归调用 4 次,最后
if (arr.length === 0) {
return true;
}
Run Code Online (Sandbox Code Playgroud)
我以为它会以
但是,该函数只被调用了 3 次!!
head: [true] tail: [true,false]
head: [true] tail: [false]
head: [false] tail: []
Run Code Online (Sandbox Code Playgroud)
这是我检查的结果
if (arr.length === 0) {
return true;
}
Run Code Online (Sandbox Code Playgroud)
这个条件甚至不起作用。
我需要帮助。
抱歉使用翻译器
| 归档时间: |
|
| 查看次数: |
45 次 |
| 最近记录: |