Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的函数返回未定义(JavaScript)_Javascript_Function_Recursion - Fatal编程技术网

我的函数返回未定义(JavaScript)

我的函数返回未定义(JavaScript),javascript,function,recursion,Javascript,Function,Recursion,我在测试一个函数,它只是做一个计算,然后测试它是否符合标准。它返回了未定义的,我不明白为什么。我在返回true之前放置了一个控制台.log(2),它将2记录到控制台,但不返回true。我是JavaScript新手,请帮忙 const cenas = [ [1, 150, 3, 0, 0, 0, 2, 0], [2, 126, 1, 0, 2, 0, 3, 0], [3, 99, 1, 0, 0, 0, 2, 0], [4, 249, 1, 0, 0, 0, 2, 0] ]; le

我在测试一个函数,它只是做一个计算,然后测试它是否符合标准。它返回了
未定义的
,我不明白为什么。我在
返回true
之前放置了一个
控制台.log(2)
,它将2记录到控制台,但不返回true。我是JavaScript新手,请帮忙

const cenas = [
  [1, 150, 3, 0, 0, 0, 2, 0],
  [2, 126, 1, 0, 2, 0, 3, 0],
  [3, 99, 1, 0, 0, 0, 2, 0],
  [4, 249, 1, 0, 0, 0, 2, 0]
];
let nC = 4;
let nA = 2;
let soma = 0;
for (let i = 0; i < nC; i++) {
  soma = soma + cenas[i][1];
}
let media = soma / nA;
let tolerancia = 0.5;
let nextStep = [1, 1, 2, 2];
let animador = 1;

const distribution = function(animador) {
  let indexes = [];
  let position = 0;
  let index = 0;
  for (let i = 0; index >= 0; i++) {
    index = nextStep.indexOf(animador, position);
    position = index + 1;
    if (index >= 0) {
      indexes.push(index);
    }
  }
  let soma = 0;
  for (i = 0; i < indexes.length; i++) {
    soma = soma + cenas[indexes[i]][1];
  }
  let criterio = Math.abs((soma - media) / media);
  if (criterio > tolerancia) {
    console.log(0)
    return false
  } else {
    console.log(1)
    if (animador >= nA) {
      console.log(2)
      return true
    } else {
      animador++;
      distribution(animador);
    }
  }
}

console.log(distribution(animador))
const cenas=[
[1, 150, 3, 0, 0, 0, 2, 0],
[2, 126, 1, 0, 2, 0, 3, 0],
[3, 99, 1, 0, 0, 0, 2, 0],
[4, 249, 1, 0, 0, 0, 2, 0]
];
设nC=4;
设nA=2;
设soma=0;
for(设i=0;i=0;i++){
索引=nextStep.indexOf(animador,位置);
位置=指数+1;
如果(索引>=0){
索引。推送(索引);
}
}
设soma=0;
对于(i=0;i公差){
console.log(0)
返回错误
}否则{
控制台日志(1)
如果(animador>=nA){
控制台日志(2)
返回真值
}否则{
animador++;
分布(动漫);
}
}
}
控制台日志(分发(animador))

在JavaScript中,每个函数的末尾都有一条不可见的线。它的
返回未定义
以防您忘记在代码的所有不同分支中使用
return
语句

如果您将适当的IDE与编码标准设置(如airbnb)一起使用,它会告诉您,
分布
被破坏,因为当
criterio>tolerencia==false
animador>=nA==false
时,它会再次出现,但对结果没有任何影响。这是死代码

想象一下你做了
Math.abs((soma-media)/media)
而不是
让criterio=Math.abs((soma-media)/media)。这有什么好处?
与
分布一样多(animador+1)
您需要以某种方式使用结果,将其设置为变量或返回

回报分布(animador+1);
以下是修复my WebStorm在启用airbnb预设的情况下使用ESLint所做的一些点后的代码:

const分布=函数(animador){
常量索引=[];
设位置=0;
设指数=0;
for(设i=0;索引>=0;i++){
索引=nextStep.indexOf(animador,位置);
位置=指数+1;
如果(索引>=0){
索引。推送(索引);
}
}
设soma=0;
for(设i=0;i公差){
控制台日志(0);
返回false;
}
控制台日志(1);
如果(animador>=nA){
控制台日志(2);
返回true;
}
分布(animador+1);
返回未定义;
};

实际上我还没有修复这个bug,但是现在更容易看到它。

预期的输出是什么?