Welcome to challenge 12! Use this thread for any and all questions relating to challenge 12.
Challenge 12 Megathread
Again, Array#filter
allows us to write a nice, compact solution:
const checkAir = (samples, threshold) => samples.filter(i => i > "d").length / samples.length < threshold ? "Clean" : "Polluted"
const checkAir = function (samples, threshold) {
return (samples.filter(sample => sample == 'dirty').length / samples.length) > threshold ? 'Polluted' : 'Clean';
}
Exact same solution hehe:
const checkAir = (samples, threshold)=>{
if ((samples.filter(sample=>sample=='dirty').length/samples.length)<threshold){
return 'Clean'
}else return 'Polluted'
}
const checkAir = function (samples, threshold) {
// Code here!
let countSample = 0;
for(var sample in samples){
if(sample === 'dirty')
countSample ++;
var pollution = countSample / samples.length;
if (pollution >= threshold){
return "Polluted";
}
else if (pollution <=threshold ){
return 'Clean';
}
}
};
I didn’t pass a test:
Failed: If there are too many dirty samples, then the function should return "Polluted"
Reason: Your code ran but did not produce the correct result.
Hint: Check to see that the ratio of dirty samples to total samples is within the threshold.
Failing Test Code:
const samples = ['clean', 'clean', 'dirty', 'clean', 'dirty', 'clean', 'clean', 'dirty', 'clean', 'dirty']
const threshold = 0.3
const result = checkAir(samples, threshold)
assert.equal(result, 'Polluted')
Expected: Polluted
Actual: Clean
you didn’t close your for loop after incrementing countSample. Also if pollution == threshold what happens? the else if should be less than instead of less than or equal to I think (:
I will show what I think it is wrong.
const checkAir = function (samples, threshold) {
// Code here!
let countSample = 0;
for(var sample in samples){
if(sample === ‘dirty’)
countSample ++;
// you need to do this after the for loop finish
var pollution = countSample / samples.length;
if (pollution >= threshold){
return "Polluted";
}
else if (pollution <=threshold ){
return 'Clean';
}
}
};
Fixed:
const checkAir = function (samples, threshold) {
// Code here!
let countSample = 0;
for(var sample in samples){
if(sample === ‘dirty’)
countSample ++;
}
var pollution = countSample / samples.length;
if (pollution >= threshold){
return "Polluted";
}
else if (pollution <=threshold ){
return 'Clean';
}
};
Great! kindly explain this line
samples.filter(i => > 'd')
your function is not returning the right result… and i notice you’re trying to return from your for loop
Check your code correctly, indent it so you will be understand what you’ve written and no where error are
We use Array#filter
to pick out the elements in samples
that satisfy the given boolean test: for every element (represented by the variable i
), check if it is greater than "d"
.
By lexicological order, "dirty" > "d"
and "clean" < "d"
, so we use this line with the .length
property to count the number of occurrences of "dirty"
in samples
.
const checkAir = function (samples, threshold) {
dirty = 0;
for (var sample in samples) {
if (sample == “dirty”) {
dirty = count++;}
let dirtnew = dirty / samples.length;
if (dirtnew > threshold) {
return ‘Polluted’;}
return ‘Clean’;
}
// Code here!
};
Thanks so much but what “d”? is it the first letter of dirty?
const checkAir = (samples, threshold) =>
samples.filter(sample => sample === "dirty").length / samples.length <
threshold
? "Clean"
: "Polluted";
what’s the count++ doing? it should be
if(sample == 'dirty){
return dirty++
}
"d"
is just a string with a single letter in it. I just use that because, alphabetically, "clean" < "d"
and "d" < "dirty"
.