如何解决变量递减如何在递归函数中工作
我知道这是一个非常初级的问题 这是场景
Let number = 5;
number - 1;
console.log(number);
解决方法
您从未更改过变量的值。您可以像这样分配递减的值:
let number = 5;
number = number - 1;
console.log(number);
也可以使用 decrement operator。
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);
更新
使用新函数,您添加到这个问题变得有点复杂,因为您现在正在处理 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,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',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'
什么是破案?中断案例用于结束递归。想象一下您的家谱,假设您想找到名为 Jane/John 的最亲近的父母(曾 ^x 奶奶/爸爸)亲戚。首先你看看你的父母,然后你看看你父母的父母,依此类推,直到你对一个 Jane/John 罚款。因此,在示例中,我们的中断案例是名称为 Jane/John 的情况。如果没有中断案例(或其他中断),您将导致无限循环。
请注意,第一个 if 语句 if (times < 0)
从未执行过,这仅仅是因为首先触发了另一个中断案例(即 if (times === 1)
),从而结束了递归。
仅从文本示例中解释/理解这是一个非常复杂的想法,并恳请您查看此示例以更好地理解递归。 https://www.freecodecamp.org/news/recursion-is-not-hard-858a48830d83/
,根据您的代码,我假设它看起来像这样 -
function repeatStringNumTimes(string,times) {
if(times < 0)
return "";
if(times === 1)
return string;
else
return string + repeatStringNumTimes(string,times - 1);
}
repeatStringNumTimes('abc',3);
第一次调用函数时,
它将具有 string = 'abc' 和 times = 3 的值,因此它将执行代码的 else 块。在那里你返回你的字符串值,你再次调用同一个函数,就是这个 "repeatStringNumTimes(string,times - 1)
" 。
这里主要是你在做 DEDUCTION 部分。只要 else 块执行,它就会不断返回新的字符串值并调用相同的函数,并从当前的次数值中减去 1,该值将通过此函数调用。
所以执行将如下所示:(现在我不更新字符串的值,只更新几次)
最初,
次数 = 3,repeatStringNumTimes(string,3);
else 块将执行,它将返回string + repeatStringNumTimes(string,2)
次 == 3,所以 (3-1 = 2) - 时间的新值。
现在,次数 = 2,repeatStringNumTimes(string,2);
再次else 块 将执行,然后返回string + repeatStringNumTimes(string,1)
次 == 2,所以 (2-1 = 1) - 时间的新值。
现在,次数 = 1,repeatStringNumTimes(string,1);
这次第二个if块将执行,它将返回string + repeatStringNumTimes(string,0)
次 == 1,所以 (1-1 = 0) - 时间的新值。
等等。
这就是它达到零(0)的方式。
请访问 Link 以更清楚地了解递归。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。