[英]How can I recurse in JS to repeat a string n times?
I am new to programming and am studying recursion at the moment.我是编程新手,目前正在研究递归。
I am trying to make the code below repeat string s
n
number of times, however, it will always repeat n - 1
times, because I need to pass a decrement to exit the recursion or I will exceed the max call stack.我试图让下面的代码重复字符串s
n
次,但是,它总是会重复n - 1
次,因为我需要通过递减来退出递归,否则我将超过最大调用堆栈。 I have been trying things with bad results until now, so I decided to ask for your help.到目前为止,我一直在尝试结果不佳的事情,所以我决定寻求您的帮助。
function repeatStr (n, s) {
while(n > 1) {
result = repeatStr(-n, s)
return s = s.concat(s)
}
}
repeatStr(3, "Hi")
What can I change to make it recurse correctly?我可以更改什么以使其正确递归?
Thanks in advance.提前致谢。 I know this is probably a very basic question but learning takes asking basic questions.我知道这可能是一个非常基本的问题,但学习需要提出基本问题。
Recursion involves making a function call itself and AVOIDS using a loop.递归涉及使 function 调用自身并避免使用循环。 You have a loop in the recursive function which is a big red flag.您在递归 function 中有一个循环,这是一个大红旗。
Do something like this: If n is 1 return s else return s concatenated to the result of the function with n - 1.执行以下操作:如果 n 为 1,则返回 s,否则将返回 s 与 n - 1 的 function 的结果相连接。
function repeatStr (n, s) {
if (n == 1)
return s;
else
return s.concat(repeatStr(n - 1, s))
}
repeatStr(3, "Hi")
Because you're new to programming and JavaScript, let's work up to the solution.因为您不熟悉编程和 JavaScript,所以让我们着手解决问题。 Start with a simple case, eg repeatStr(3, "Hi")
.从一个简单的案例开始,例如repeatStr(3, "Hi")
。 One simple answer may be:一个简单的答案可能是:
function repeatStr(n, s) {
return "HiHiHi";
}
Here, we assume what the inputs are always 3 and "Hi".在这里,我们假设输入总是 3 和“Hi”。 We don't have a while loop, and we don't have recursion.我们没有while循环,也没有递归。 We have the correct answer for those specific inputs but no other inputs are supported.对于这些特定输入,我们有正确的答案,但不支持其他输入。
Let's make the answer a little bit harder:让我们把答案变得更难一点:
function repeatStr(n, s) {
return "Hi" + "Hi" + "Hi";
}
Here, again, we are assuming the inputs are 3 and "Hi".在这里,我们再次假设输入是 3 和“Hi”。 We still don't have a while loop nor do we have recursion.我们仍然没有 while 循环,也没有递归。
Okay, let's start using one of your inputs:好的,让我们开始使用您的输入之一:
function repeatStr(n, s) {
return s + s + s;
}
Here, we are finally using the string that's passed in as s
.在这里,我们最终使用了作为s
传入的字符串。 We can handle inputs other than "Hi"
to generate a correct answer.我们可以处理"Hi"
以外的输入来生成正确的答案。 But, we're still assuming the number input is 3.但是,我们仍然假设输入的数字是 3。
Finally, let's have a look at n
:最后,让我们看一下n
:
function repeatStr(n, s) {
let result = "";
while (n > 0) {
n = n - 1;
result = result + s;
}
return result;
}
Okay, here we take both inputs n
and s
into consideration and we solve the problem by appending s
exactly the number n
times needed.好的,这里我们将输入n
和s
都考虑在内,我们通过将s
精确地附加n
次来解决问题。 This is a while loop solution, not a recursion solution.这是一个 while 循环解决方案,而不是递归解决方案。 Our while loop has the action result = result + s;
我们的 while 循环有动作result = result + s;
repeated exactly n
times where we use n
as a countdown and stop when we reach 0.精确地重复n
次,我们使用n
作为倒计时并在到达 0 时停止。
Now, we have all that background, let's look at one version of the recursion solution.现在,我们有了所有的背景知识,让我们看一下递归解决方案的一个版本。
function repeatStr(n, s) {
if (n <= 0) {
return "";
}
return s + repeat(n - 1, s);
}
Even in recursion form we retain the countdown feature.即使在递归形式中,我们也保留倒计时功能。 This time the countdown is used to drive the recursion instead of a while loop.这次倒计时用于驱动递归而不是while循环。 We still counting down to 0 where we have an if-return guard condition that's needed to terminate the recursion.我们仍然倒计时到 0,我们有一个 if-return 保护条件,需要终止递归。 ie when n <= 0
, we exit with the simple empty string ""
.即当n <= 0
时,我们以简单的空字符串""
退出。 Then for the more complex case, it is solving any nth version by expressing in terms of the (n-1)
th version.然后对于更复杂的情况,它通过用第(n-1)
个版本表示来解决任何第 n 个版本。 Another way of looking at it is this:另一种看待它的方式是:
repeatStr(3, "Hi") === "Hi" + repeatStr(2, "Hi")
=== "Hi" + "Hi" + repeatStr(1, "Hi")
=== "Hi" + "Hi" + "Hi" + repeatStr(0, "Hi")
=== "Hi" + "Hi" + "Hi" + ""
If you want to get a little clever, JavaScript has a conditional which can be used in place of your if statement:如果你想变得聪明一点,JavaScript 有一个条件可以用来代替你的 if 语句:
function repeatStr(n, s) {
return (n <= 0) ? "" : s + repeat(n - 1, s);
}
Hope that makes sense.希望这是有道理的。
Adding a decomposed approach to the other nice answers in this thread -在此线程中为其他不错的答案添加分解方法-
const concat = a => b => a.concat(b) const repeat = n => f => x => n && repeat(n - 1)(f)(f(x)) || x const hello = repeat(3)(concat("Hi")) console.log(hello("Alice")) console.log(hello(""))
HiHiHiAlice
HiHiHi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.