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

Time:11-14

Whenever I run my code I get the answer that I want. However, there is "undefined" under each answer. Any idea how to fix and prevent this from happening? I'm unsure as to why this is popping up as it's clearly giving me a defined value?

In case it matters, new to Javascript.

Thanks

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

CodePudding user response:

Remove the console.log after return

CodePudding user response:

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
))
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Instead of returning the console.log(something), return the string instead. Else remove the console.log while calling the function.

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)
);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

As others have pointed out, it's because you're returning the output of console.log and then console.logging that. You see the answer in the log because of the call to console.log inside the function, and the second one just prints undefined, because that's what the function is returning.

But you have a little bit of unnecessary code. Look at this:

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
))
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

You can use filter() on the input array to generate a new array that only has the dirty elements, and then divide the length of that new array by the length of the initial array and compare that to the threshold. In my one line function, I return true/false, which is usually what I would generally consider the expected output of a function like that. If you want to coerce that to a friendly value, you can, either in the function itself (I'm going to use the ternary operator on the boolean):

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
))
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Or separate the function and the string selection:

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');
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

If all you are ever going to do with this function is generate a display friendly string, then go ahead and put the string conversion in the function. But if you are going to use the function in some kind of decision making, then true/false is the way to go, because later you can say something like:

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

and it makes the code a little more readable. If you were to name the function itself dirty() -- instead of checkAir() -- then you could write something like this:

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