Created
June 30, 2020 19:41
-
-
Save andreiskandar/cf3d7b2d4826e000eee2318ef804745c to your computer and use it in GitHub Desktop.
Checking air pollution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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