Javascript 有没有一种方法可以递增地增加递归中的计数?

Javascript 有没有一种方法可以递增地增加递归中的计数?,javascript,recursion,towers-of-hanoi,Javascript,Recursion,Towers Of Hanoi,有没有办法增加河内塔挑战赛的参赛人数? 目前,这个数字并没有递增。有趣的是,它从五点开始跳来跳去。请参见下面的输出 我尝试在函数内添加一个let count=0,在IF语句内和递归函数外添加++count,但没有成功 工作代码块: const move = (n, source, destination, buffer, count) => { // Verify that there are disks. if (n > 0) { // Move one dis

有没有办法增加河内塔挑战赛的参赛人数?

目前,这个数字并没有递增。有趣的是,它从五点开始跳来跳去。请参见下面的输出

我尝试在函数内添加一个
let count=0
,在IF语句内和递归函数外添加
++count
,但没有成功

工作代码块:

const move = (n, source, destination, buffer, count) => {

  // Verify that there are disks.
  if (n > 0) {

    // Move one disk to buffer rod.
    move(n - 1, source, buffer, destination, ++count);

    // Move nth disk from source to destination.
    destination.push(source.pop())

    // Display progress
    console.log(count, A, B, C, "\n--------------------------")

    // Move the disk (n - 1) from the buffer to the destination.
    move(n - 1, buffer, destination, source, ++count);
  }
};

// Disk starting position
const A = [5, 4, 3, 2, 1]
const B = []
const C = []

// Invoke
move(5, A, C, B, 0);
输出:

5 [ 5, 4, 3, 2 ] [] [ 1 ] 
--------------------------
4 [ 5, 4, 3 ] [ 2 ] [ 1 ] 
--------------------------
6 [ 5, 4, 3 ] [ 2, 1 ] [] 
--------------------------
3 [ 5, 4 ] [ 2, 1 ] [ 3 ] 
--------------------------
6 [ 5, 4, 1 ] [ 2 ] [ 3 ] 
--------------------------
5 [ 5, 4, 1 ] [] [ 3, 2 ] 
--------------------------
7 [ 5, 4 ] [] [ 3, 2, 1 ] 
--------------------------
2 [ 5 ] [ 4 ] [ 3, 2, 1 ] 
--------------------------
6 [ 5 ] [ 4, 1 ] [ 3, 2 ] 
--------------------------
5 [ 5, 2 ] [ 4, 1 ] [ 3 ] 
--------------------------
7 [ 5, 2, 1 ] [ 4 ] [ 3 ] 
--------------------------
4 [ 5, 2, 1 ] [ 4, 3 ] [] 
--------------------------
7 [ 5, 2 ] [ 4, 3 ] [ 1 ] 
--------------------------
6 [ 5 ] [ 4, 3, 2 ] [ 1 ] 
--------------------------

返回
count
的值并将其分配回变量

constmove=(n、源、目标、缓冲区、计数)=>{
//验证是否存在磁盘。
如果(n>0){
//将一个磁盘移到缓冲杆上。
计数=移动(n-1,源,缓冲区,目标,++计数);
//将第n个磁盘从源移动到目标。
destination.push(source.pop())
//显示进度
log(count、JSON.stringify(A)、JSON.stringify(B)、JSON.stringify(C),“\n--------------------------------------”)
//将磁盘(n-1)从缓冲区移动到目标。
计数=移动(n-1,缓冲区,目的地,源,++计数);
}
返回计数;
};
//发票
常数A=[5,4,3,2,1]
常数B=[]
常数C=[]

移动(5,A,C,B,0)是否使用预先指定的参数?因此,在函数签名中将count替换为count=0@Berk我尝试了count=0,结果是一样的。即使调用包含0,添加一个预先指定的参数是否会改变结果?我的意思是,它从0开始,然后分别递增,如果在函数中将其赋值为0,则每次递归发生时它都将为0,这样可以递增值,这是我的方法。