【问题标题】:My code seems to work - but there is an "undefined" after each answer我的代码似乎有效 - 但每个答案后都有一个“未定义”
【发布时间】:2021-11-14 06:46:28
【问题描述】:

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

如果它很重要,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
))

【问题讨论】:

    标签: javascript loops for-loop undefined


    【解决方案1】:

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

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

    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() 来生成一个仅包含脏元素的新数组,然后将该新数组的长度除以初始数组的长度并将其与阈值进行比较。在我的单行函数中,我返回真/假,这通常是我通常认为的函数的预期输出。如果您想将其强制为友好值,您可以在函数本身中(我将在布尔值上使用三元运算符):

    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*/ }
    

    【讨论】:

      【解决方案2】:

      返回后删除console.log

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案3】:

      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
      ))

      【讨论】:

        【解决方案4】:

        不返回console.log(something),而是返回字符串。 否则在调用函数时删除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)
        );

        【讨论】:

          猜你喜欢
          • 2015-08-21
          • 1970-01-01
          • 2022-11-17
          • 2016-03-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-11
          • 2021-02-01
          相关资源
          最近更新 更多