在递归函数调用中增加计数器 [英] Incrementing a counter in recursive function calls

查看:92
本文介绍了在递归函数调用中增加计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力增加javascript中的基本计数器。



我要实现什么?



我需要在 foreach 循环内使用一个计数器。目标是能够每次触发 //写smthg 时进行计数。



下面是<我正在使用的代码的strong>更新版本。目前,它返回奇怪的数字序列。我想每次触发递归循环时都会重置它。我不知道该如何解决它,这是一个基本的javascript问题,但是当我通过实验和自己学习时,有时我需要向社区提问。



< pre class = snippet-code-js lang-js prettyprint-override> function walk(dir,counter = 0){fs.readdirSync(dir).forEach(file => {让fullPath = path.join(dir,file); if(fs.lstatSync(fullPath).isDirectory()){counter = walk(fullPath,counter); walk(fullPath,counter); console.log('dir');} else {let size = fs.statSync(fullPath).size; //获取文件listFiles.write(fullPath +( + size +)\n); //将文件的路径和大小写入copyList.xml ++ counter; console.log(counter);}});返回计数器;} walk(copyFrom); //启动函数 walk



获得的序列:

  2,3,4,5,6,7,dir,5,6,8,9,10,11,12,13 ,dir,11 



这里是完整答案



< pre class = snippet-code-js lang-js prettyprint-override> function walk(dir){令n = 0;函数walk(dir){fs.readdirSync(dir).forEach(file => {++ n; console.log(n); let fullPath = path.join(dir,file); if(fs.lstatSync(fullPath ).isDirectory()){--n; walk(fullPath); console.log('dir');} else {let size = fs.statSync(fullPath).size; //获取文件listFiles.write( fullPath +( + size +)\n); //将文件路径和大小写入copyList.xml}}); } return walk(dir);}

解决方案

使用助手。函数 walk 使词法变量 n 和函数 walk 会在递归调用期间遮住被调用函数。它可能具有 walk 的原始内容,而外部函数只是返回调用它本身的结果。

  function walk(dir){
让n = 0; //计数器变量
函数walk(dir){
dir.forEach(file => {
++ n;
console.log(n);
if(true){
//递归循环
} else {
//写smthg
}
});
}
return walk(dir);
}


I'm struggling on how to increment a basic counter in javascript.

What do I want to achieve ?

I need a counter inside a foreach loop. The goal is to be able to count each time the //Write smthg is triggered.

Below is the updated version of the code I'm using. For the moment, it returns weird sequences of numbers. I guess it is resetted each time the recursive loop is triggered. I do not know how to correct it, suppose it's a basic javascript problem but as I'm learning through experimenting and on my own, I sometimes need to ask question to the community.

function walk(dir, counter = 0) {

  fs.readdirSync(dir).forEach(file => {

    let fullPath = path.join(dir, file);

    if (fs.lstatSync(fullPath).isDirectory()) {
      counter = walk(fullPath, counter);
      walk(fullPath, counter);
      console.log('dir');
    } else {
      let size = fs.statSync(fullPath).size; // Get size of file
      listFiles.write(fullPath + " (" + size + ")\n"); // Write file path and size into copyList.xml
      ++counter;
      console.log(counter);
    }

  });
  return counter;
}

walk(copyFrom); // Starts function "walk"

Sequences obtained :

2,3,4,5,6,7,dir,5,6,8,9,10,11,12,13,dir,11

Here is the complete answer

function walk(dir) {
  let n = 0;

  function walk(dir) {

    fs.readdirSync(dir).forEach(file => {

      ++n;
      console.log(n);
      let fullPath = path.join(dir, file);

      if (fs.lstatSync(fullPath).isDirectory()) {
        --n;
        walk(fullPath);
        console.log('dir');
      } else {
        let size = fs.statSync(fullPath).size; // Get size of file
        listFiles.write(fullPath + " (" + size + ")\n"); // Write file path and size into copyList.xml
      }

    });
  }
  return walk(dir);
}

解决方案

Use a helper. The function walk makes the lexical variable n and a function walk that shadows the called fucntion for the duration of the recursive calls. It may have the original content of walk and the outer function just returns the result of calling it as itself was called.

function walk(dir) {
  let n = 0; //Counter variable
  function walk(dir) {
    dir.forEach(file => {
      ++n;
      console.log(n);
      if (true) {
        //Recurse loop
      } else {
        //Write smthg
      }
    });
  }
  return walk(dir);
}

这篇关于在递归函数调用中增加计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆