每當我運行我的代碼時,我都會得到我想要的答案。但是,每個答案下都有“未定義”。知道如何解決和防止這種情況發生嗎?我不確定為什么會出現這種情況,因為它顯然給了我一個明確的價值?
萬一重要,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
))
uj5u.com熱心網友回復:
正如其他人指出的那樣,這是因為您要回傳 console.log 的輸出,然后是 console.logging 。由于在函式內部呼叫了 console.log,您會在日志中看到答案,而第二個只是列印 undefined,因為這就是函式回傳的內容。
但是你有一些不必要的代碼。看這個:
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
))
您可以在輸入陣列上使用 filter() 生成一個只有臟元素的新陣列,然后將該新陣列的長度除以初始陣列的長度,并將其與閾值進行比較。在我的單行函式中,我回傳真/假,這通常是我通常認為的函式的預期輸出。如果您想將其強制為友好值,您可以在函式本身中(我將在布林值上使用三元運算子):
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
))
或者將函式和字串選擇分開:
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');
如果您使用此函式要做的只是生成一個顯示友好的字串,那么繼續將字串轉換放入函式中。但是如果你打算在某種決策中使用這個函式,那么 true/false 就是要走的路,因為以后你可以這樣說:
dirty = checkAir(samples, threshold);
if(dirty) { /*what to do if dirty*/ }
else { /*what to do if clean*/ }
它使代碼更具可讀性。如果你將函式本身命名為dirty()——而不是checkAir()——那么你可以這樣寫:
if(dirty(samples, threshold)) { /*dirty response*/ }
else { /*clean response*/ }
uj5u.com熱心網友回復:
回傳后洗掉console.log
uj5u.com熱心網友回復:
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
))
uj5u.com熱心網友回復:
而不是回傳console.log(something),而是回傳字串。否則console.log在呼叫函式時洗掉。
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)
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/357337.html
標籤:javascript 循环 for循环 不明确的
上一篇:如何獲取for回圈的最后一項
