我的代码似乎有效 - 但每个答案后都有一个 "undefined"

My code seems to work - but there is an "undefined" after each answer

每当我 运行 我的代码时,我都会得到我想要的答案。但是,每个答案下都有“未定义”。知道如何解决和防止这种情况发生吗?我不确定为什么会弹出这个消息,因为它显然给了我一个定义值?

以防万一,Javascript 的新手。

谢谢

const checkAir = function (samples, threshold) {
  let numb = samples.length 
  let dirtyCount = ""


  for (let i = 0 ; i < samples.length ; i++) {
    if (samples[i] === 'dirty'){
      dirtyCount++
    }
  }
  if (dirtyCount / numb >= threshold){
    return console.log("Polluted")
  } else {
    return console.log("Clean")
  }
}

console.log(checkAir(
  ['clean', 'clean', 'dirty', 'clean', 'dirty', 'clean', 'clean', 'dirty', 'clean', 'dirty'],
  0.3
));


console.log(checkAir(
  ['dirty', 'dirty', 'dirty', 'dirty', 'clean'],
  0.25
));

console.log(checkAir(
  ['clean', 'dirty', 'clean', 'dirty', 'clean', 'dirty', 'clean'],
  0.9
))

而不是return中的console.log(something),而是return字符串。 否则在调用函数时删除 console.log

const checkAir = function (samples, threshold) {
  let numb = samples.length;
  let dirtyCount = "";
  for (let i = 0; i < samples.length; i++) {
    if (samples[i] === "dirty") {
       dirtyCount++;
    }
  }
  if (dirtyCount / numb >= threshold) {
     return "Polluted";
  } else {
     return "Clean";
  }
};

console.log(
  checkAir(
[
  "clean",
  "clean",
  "dirty",
  "clean",
  "dirty",
  "clean",
  "clean",
  "dirty",
  "clean",
  "dirty"
],
0.3
  )
);

console.log(checkAir(["dirty", "dirty", "dirty", "dirty", "clean"], 0.25));
console.log(
  checkAir(["clean", "dirty", "clean", "dirty", "clean", "dirty", "clean"], 0.9)
);

删除return

之后的console.log

const checkAir = function(samples, threshold) {
  let numb = samples.length
  let dirtyCount = ""


  for (let i = 0; i < samples.length; i++) {
    if (samples[i] === 'dirty') {
      dirtyCount++
    }
  }
  if (dirtyCount / numb >= threshold) {
    return "Polluted"
  } else {
    return "Clean"
  }
}

console.log(checkAir(
  ['clean', 'clean', 'dirty', 'clean', 'dirty', 'clean', 'clean', 'dirty', 'clean', 'dirty'],
  0.3
));


console.log(checkAir(
  ['dirty', 'dirty', 'dirty', 'dirty', 'clean'],
  0.25
));

console.log(checkAir(
  ['clean', 'dirty', 'clean', 'dirty', 'clean', 'dirty', 'clean'],
  0.9
))

正如其他人所指出的,这是因为您要 returning console.log 的输出,然后 console.logging 那个。由于在函数内部调用 console.log,您会在日志中看到答案,而第二个只是打印未定义,因为这就是函数 returning.

但是你有一些不必要的代码。看看这个:

const checkAir = (samples, threshold) => {
  return samples.filter(s=>s=='dirty').length / samples.length > threshold;
}

console.log(checkAir(
  ['clean', 'clean', 'dirty', 'clean', 'dirty', 'clean', 'clean', 'dirty', 'clean', 'dirty'],
  0.3
));


console.log(checkAir(
  ['dirty', 'dirty', 'dirty', 'dirty', 'clean'],
  0.25
));

console.log(checkAir(
  ['clean', 'dirty', 'clean', 'dirty', 'clean', 'dirty', 'clean'],
  0.9
))

您可以在输入数组上使用 filter() 来生成一个只有脏元素的新数组,然后将新数组的长度除以初始数组的长度并将其与阈值进行比较。在我的一行函数中,我 return true/false,这通常是我通常认为的这样一个函数的预期输出。如果您想将其强制转换为一个友好的值,您可以在函数本身中使用(我将在布尔值上使用三元运算符):

const checkAir = (samples, threshold) => {
  let polluted = samples.filter(s=>s=='dirty').length / samples.length > threshold;
  return polluted ? 'Polluted' : 'Clean';
}

console.log(checkAir(
  ['clean', 'clean', 'dirty', 'clean', 'dirty', 'clean', 'clean', 'dirty', 'clean', 'dirty'],
  0.3
));


console.log(checkAir(
  ['dirty', 'dirty', 'dirty', 'dirty', 'clean'],
  0.25
));

console.log(checkAir(
  ['clean', 'dirty', 'clean', 'dirty', 'clean', 'dirty', 'clean'],
  0.9
))

或者将函数和字符串选择分开:

const checkAir = (samples, threshold) => {
  return samples.filter(s=>s=='dirty').length / samples.length > threshold;
}

let check = checkAir(
  ['clean', 'clean', 'dirty', 'clean', 'dirty', 'clean', 'clean', 'dirty', 'clean', 'dirty'],
  0.3
);
console.log(check ? 'Polluted' : 'Clean');


check = checkAir(
  ['dirty', 'dirty', 'dirty', 'dirty', 'clean'],
  0.25
);
console.log(check ? 'Polluted' : 'Clean');

check = checkAir(
  ['clean', 'dirty', 'clean', 'dirty', 'clean', 'dirty', 'clean'],
  0.9
);
console.log(check ? 'Polluted' : 'Clean');

如果您要使用此函数做的只是生成一个显示友好的字符串,那么请继续将字符串转换放入函数中。但是,如果您要在某种决策中使用该函数,那么 true/false 是最佳选择,因为稍后您可以这样说:

dirty = checkAir(samples, threshold);
if(dirty) { /*what to do if dirty*/ }
else { /*what to do if clean*/ }

它使代码更具可读性。如果您要将函数本身命名为 dirty()——而不是 checkAir()——那么您可以这样写:

if(dirty(samples, threshold)) { /*dirty response*/ }
else { /*clean response*/ }