像计算机一样思考。
let number = 5;
number - 1;
console.log(number);
JavaScript 会将第二行中的number 替换为number 的最新变量赋值,因此基本上它会变成...
let number = 5;
5 - 1; // this does no assignment so basically useless
console.log(5);
但是,如果您要重新分配变量 number,它将看起来像这样...
let number = 5;
number = number - 1; // or number--
console.log(number);
// you can think of this like...
let number = 5;
number = 5 - 1; // number is now 4
console.log(4);
更新
使用新功能,您添加到这个问题变得有点复杂,因为您现在处理的是recursion。就好像我们是“计算机”(又名调试)一样单步执行...
// actual function
function repeatStringNumTimes(string, times) {
if (times < 0)
return "";
if (times === 1)
return string;
else
return string + repeatStringNumTimes(string, times - 1);
}
repeatStringNumTimes("abc", 3);
// Walking through this function from the first invocation above
// first function call
function repeatStringNumTimes(string, times) { // string = 'abc' and times = 3
if (3 < 0) // false - thus skipped
return "";
if (3 === 1) // false - thus skipped
return 'abc';
else // run because all others were false/skipped
// Note the code below will not return until the repeatStringNumTimes below returns
return 'abc' + repeatStringNumTimes('abc', 3 - 1);
}
// second function call - first recursive call
function repeatStringNumTimes(string, times) { // string = 'abc' and times = 2 (i.e. 3 - 1)
if (2 < 0) // false - thus skipped
return "";
if (2 === 1) // false - thus skipped
return 'abc';
else // run because all others were false/skipped
// Note the code below will not return until the repeatStringNumTimes below returns
return 'abc' + repeatStringNumTimes('abc', 2 - 1);
}
// third function call - second recursive call
function repeatStringNumTimes(string, times) { // string = 'abc' and times = 1 (i.e. 2 - 1)
if (1 < 0) // false - thus skipped
return "";
if (1 === 1) // true - thus executed
// This block is what is known as a "break case" see below
return 'abc'; // returns 'abc' end of recursion!
else // skipped because previous if statement returned 'abc'
return 'abc' + repeatStringNumTimes('abc', 1 - 1);
}
// going back to the second function call, which we simplified to
function repeatStringNumTimes(string, times) { // string = 'abc' and times = 2
return 'abc' + repeatStringNumTimes('abc', 2 - 1);
}
// but we know that the return value of `repeatStringNumTimes('abc', 2 - 1)` is just 'abc', thus we can say
function repeatStringNumTimes(string, times) { // string = 'abc' and times = 2
return 'abc' + 'abc'; // or 'abcabc'
}
// going back to the first function call, which we simplified to
function repeatStringNumTimes(string, times) { // string = 'abc' and times = 3
return 'abc' + repeatStringNumTimes('abc', 3 - 1);
}
// but we know that the return value of `repeatStringNumTimes('abc', 3 - 1)` is just 'abcabc', thus we can say
function repeatStringNumTimes(string, times) { // string = 'abc' and times = 3
return 'abc' + 'abcabc'; // or 'abcabcabc'
}
// so the final value of this statement is 'abcabcabc' or...
repeatStringNumTimes("abc", 3); // returns 'abcabcabc'
什么是break case?中断情况用于结束递归。想象一下你的家谱,假设你想找到你最亲近的父母(伟大的 ^ x 祖母/爸爸)亲戚,名叫 Jane/John。首先你看看你的父母,然后你看看你父母的父母等等,直到你罚款简/约翰。因此,在示例中,我们的中断案例是姓名为 Jane/John。如果没有中断情况(或其他中断),您将导致无限循环。
请注意,第一个 if 语句 if (times < 0) 从未执行过,这仅仅是因为首先触发了另一个中断情况(即 if (times === 1)),从而结束了递归。
这是一个非常复杂的想法,仅从文本示例中解释/理解,并恳请您查看此示例以更好地理解递归。 https://www.freecodecamp.org/news/recursion-is-not-hard-858a48830d83/