試圖創建一個函式,將我的年齡與其他人的年齡進行比較。顯然,我遺漏了一些東西,因為無論什么輸入輸出總是相同的。請幫忙。
This is what I have so far:
function compareAge(name,age){
let myAge=27;
if (age = myAge){
return `${name} is the same age as me.`
}else if (age < myAge){
return `${name} is younger than me.`
}else if (age > myAge){
return `${name} is older than me.`
}
}
console.log(compareAge('Kat',2));
uj5u.com熱心網友回復:
在這個 if 陳述句中,您正在分配 ifage的值myAge并將結果傳遞給 if。
if (age = myAge){
在 Javascript 中,任何整數的布林值都是 true。
console.log(Boolean(27));
//prints true
要實際比較這些值age,myAge您需要使用雙等號運算子。像這樣:
function compareAge(name,age){
let myAge=27;
if (age == myAge){
return `${name} is the same age as me.`
}else if (age < myAge){
return `${name} is younger than me.`
}else if (age > myAge){
return `${name} is older than me.`
}
}
console.log(compareAge('Kat',2));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/529180.html
上一篇:計算完成給定任務的最短時間
