使用递归函数进行双倍倒计时?

发布于 2025-01-21 00:40:01 字数 977 浏览 0 评论 0原文

我正在尝试使用JavaScript编写递归,而无需使用全局变量,但我不确定该程序是否可以像Desire Output一样。

问题是“给出了一个非负整数n,在输出中创建一个双重倒计数模式”。 例子: 如果您运行了Doublecountdown(5),您还将获得以下输出:

5
4
3
2
1
4
3
2
1
0

我尝试了以下方式: 这样,下降到输出而不是确切的输出:

double_countdown(5);
function double_countdown(n){
    if(n == 0){
        console.log(n);
        return
    }
    console.log(n);
    double_countdown(n-1);
    console.log(n);
}

通过这种方式,我将全球用于所需的输出,但不是标准方式。

let second_n = 0;
let double_count =0;
double_countdown(5);
function double_countdown(n){
    if(n == 0 && double_count == 0){
        double_count = 1;
        double_countdown(second_n-1);
        console.log(n);
    }
    if(n == 0){
        return
    }else{
        second_n +=1;
    }
    console.log(n);
    double_countdown(n-1);
}

I'm trying to write a recursion using JavaScript, without using global variables but i am not sure that the program can be possible like desire output.

Question is "Given a non-negative integer n, create a double countdown-pattern in the output."
Example:
If you ran doubleCountdown(5) you would also get the following output:

5
4
3
2
1
4
3
2
1
0

I have tried following ways:
In this way getting down to up output but not exact output:

double_countdown(5);
function double_countdown(n){
    if(n == 0){
        console.log(n);
        return
    }
    console.log(n);
    double_countdown(n-1);
    console.log(n);
}

In this ways, i have used global for desired output but not standard way.

let second_n = 0;
let double_count =0;
double_countdown(5);
function double_countdown(n){
    if(n == 0 && double_count == 0){
        double_count = 1;
        double_countdown(second_n-1);
        console.log(n);
    }
    if(n == 0){
        return
    }else{
        second_n +=1;
    }
    console.log(n);
    double_countdown(n-1);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终止放荡 2025-01-28 00:40:01

我会将单个倒计时编写为递归函数,并让双倒计时函数只需调用它两次。这使得所有部件都很简单。

const countdown = (s, e = 0) => 
  s < e ? [] : [s, ...countdown (s -1, e)]

const doubleCountdown = (n) => 
  [...countdown (n, 1), ...countdown (n - 1, 0)]

console .log (doubleCountdown (5))

然后,您可以按照您想要的方式记录家庭作业的答案。也许类似于 for (let n of doubleCountdown (5)) {console .log (n)}

I would write the single countdown as a recursive function and have a double countdown function simply call it twice. This keeps all the parts simple.

const countdown = (s, e = 0) => 
  s < e ? [] : [s, ...countdown (s -1, e)]

const doubleCountdown = (n) => 
  [...countdown (n, 1), ...countdown (n - 1, 0)]

console .log (doubleCountdown (5))

You can then log the answer however you want for your homework. Perhaps something like for (let n of doubleCountdown (5)) {console .log (n)}

千纸鹤 2025-01-28 00:40:01

通过提供和使用更多的参数来跟踪其递归状态的方式实现递归函数,而不仅仅是所需的第一个参数(这是当前的计数值)。

因此,将实现一个能够下计数周期n次的函数。 double_countdown分别CountDownTwice函数可以从这种通用实现中派生。

function countDownNTimes(value = 0, times = 1, initialValue) {
  // initially invoked with an `undefined` value for `initialValue`.
  initialValue ??= value;

  console.log(value);

  if (times >= 2) {
    if (value <= 1) {

      // does trigger a new recursion cycle with minimized boundaries.
      countDownNTimes(initialValue - 1, --times, initialValue);
    } else {

      // stays within the current recursion of minimized boundaries.
      countDownNTimes(--value, times, initialValue);
    }
  } else if ((times >= 1) && (value >= 1))  {

    // stays within the current, final recursion of reastablished boundaries.
    countDownNTimes(--value, times, initialValue);
  }
}
const countDownTwice = value => countDownNTimes(value, 2);

console.log('... countDownTwice(5) ...');
countDownTwice(5);

console.log('... countDownNTimes(3, 3) ...');
countDownNTimes(3, 3);
.as-console-wrapper { min-height: 100%!important; top: 0; }

Implement the recursive function in a way that it keeps track of its recursion state by providing and working with more parameters than just the required first parameter (which is the current count down value).

Thus one would implement e.g. a function which is capable of running the count down cycle N times. And a double_countdown respectively countDownTwice function could be derived from such a generic implementation.

function countDownNTimes(value = 0, times = 1, initialValue) {
  // initially invoked with an `undefined` value for `initialValue`.
  initialValue ??= value;

  console.log(value);

  if (times >= 2) {
    if (value <= 1) {

      // does trigger a new recursion cycle with minimized boundaries.
      countDownNTimes(initialValue - 1, --times, initialValue);
    } else {

      // stays within the current recursion of minimized boundaries.
      countDownNTimes(--value, times, initialValue);
    }
  } else if ((times >= 1) && (value >= 1))  {

    // stays within the current, final recursion of reastablished boundaries.
    countDownNTimes(--value, times, initialValue);
  }
}
const countDownTwice = value => countDownNTimes(value, 2);

console.log('... countDownTwice(5) ...');
countDownTwice(5);

console.log('... countDownNTimes(3, 3) ...');
countDownNTimes(3, 3);
.as-console-wrapper { min-height: 100%!important; top: 0; }

人间☆小暴躁 2025-01-28 00:40:01

最后,经过多次尝试,我得到了答案:

double_countdown(5);
function double_countdown(n,sc=0){
    if(sc == 0) sc = n;
    if(n == 0 ) return ;
    console.log(n);
    double_countdown(n-1,sc);
    console.log(sc-n);
}

Finally, after trying many times, I got the answer:

double_countdown(5);
function double_countdown(n,sc=0){
    if(sc == 0) sc = n;
    if(n == 0 ) return ;
    console.log(n);
    double_countdown(n-1,sc);
    console.log(sc-n);
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文