【发布时间】: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