变量递减在递归函数中是如何工作的



我知道这是一个非常初级的问题这是的场景

Let number = 5; 
number - 1;
console.log(number);

它显示5,为什么不显示4;

您从未更改变量的值。您可以分配递减值,如下所示:

let number = 5; 
number = number - 1;
console.log(number);

也可以使用递减运算符。

let number = 5; 
--number;
console.log(number);

let number = 5; 
number--;
console.log(number);

如果您想查看或使用操作的更改,请始终在新变量或同一变量中分配值。如果你把它分配给一个新的变量,那么它就会变成:

Let number = 5; 
let newNumber = number - 1;
console.log(number); -- output 5
console.log(newNumber); --output 4

如果你把它分配给同一个变量,它就会变成

Let number = 5; 
number = number - 1;
console.log(number); -- output 4

数字-1实际上是4。但这个数字仍然是5。你需要写

number = number -1;

成为核心

您在不分配给变量number - 1;的情况下递减。相反,在您的情况下,有两种方法可以减少

案例1:

数字=数字-1;

案例2:

数字--;

两者都可以正常工作。

像计算机一样思考。

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);

更新

对于新函数,您添加到这个问题中,它变得有点复杂,因为您现在正在处理递归。走过这一步,就好像我们是";"计算机";(又名调试(。。。

// 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来结束递归。想象一下你的家谱,假设你想找到你最亲密的父母(曾祖母/父亲(亲戚,名叫简/约翰。首先你看你的父母,然后你看你父母的父母,以此类推,直到你罚款一个简/约翰。所以在例子中,我们的破格是当名字是Jane/John时。如果没有break case(或其他break(,您将导致无限循环。

请注意,第一个if语句if (times < 0)从未执行过,这只是因为首先触发了另一个中断情况(即if (times === 1)(,从而结束了递归。

这是一个非常复杂的想法,仅从文本示例中进行解释/理解,并恳请您查看此示例以更好地理解递归。https://www.freecodecamp.org/news/recursion-is-not-hard-858a48830d83/

最新更新