我正在嘗試解決這個簡單的 js 函式問題,但它給出了未定義的輸出以及正確的答案。我知道這可能是因為回傳值問題,但在這種情況下如何解決?
function findMax(a,b,c){
if(a>b && a>c){
console.log("a" " is max")
}else if(a<b && b>c){
console.log("b" " is max")
}else{
console.log("c" " is max")
}
}
console.log(findMax(2,4,5))
uj5u.com熱心網友回復:
您的控制臺記錄未定義的回傳值。只需擺脫最后一個控制臺日志。
function findMax(a,b,c){
if(a>b && a>c){
console.log("a" " is max")
}else if(a<b && b>c){
console.log("b" " is max")
}else{
console.log("c" " is max")
}
}
findMax(2,4,5);
或者你可以回傳字串
function findMax(a,b,c){
if(a>b && a>c){
return "a" " is max";
}else if(a<b && b>c){
return "b" " is max";
}else{
return "c" " is max";
}
}
console.log(findMax(2,4,5))
uj5u.com熱心網友回復:
您需要將值回傳到最外層。您可以只回傳簡單的字串,因此您可以記錄或對回傳的值執行其他操作。
function findMax(a, b, c) {
if (a > b && a > c) {
return "a" " is max"
} else if (a < b && b > c) {
return "b" " is max"
} else {
return "c" " is max"
}
}
console.log(findMax(2, 4, 5))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/466354.html
標籤:javascript
