Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreiskandar/cf3d7b2d4826e000eee2318ef804745c to your computer and use it in GitHub Desktop.
Save andreiskandar/cf3d7b2d4826e000eee2318ef804745c to your computer and use it in GitHub Desktop.
Checking air pollution
/*
For this challenge we will implement a function called checkAir(), which will check a collection of air samples.
The function will take in two arguments. The first argument is an array of strings, where each string represents
a small air sample that is either clean or dirty. The second argument is a number representing the highest
acceptable amount of dirty samples. For example, a threshold of 0.4 means that there must be less than 40% of
total samples classified as dirty for our air to be considered clean. Our function must return Polluted if there
are too many dirty air samples, or Clean if the proportion of dirty samples is below the threshold.
*/
const checkAir = function (samples, threshold) {
let count = samples.filter(i => {
return i !== 'clean'
});
if((count.length / samples.length) < threshold){
return 'Clean';
} else return 'Polluted';
};
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
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment