亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我正在嘗試解決的初級 Javascript 函數問題

我正在嘗試解決的初級 Javascript 函數問題

繁星淼淼 2023-06-15 15:39:24
我要解決的問題如下:我經常糾結于在某一天我應該穿短褲還是長褲。請給我寫一個名為的函數來幫助我做出決定isShortsWeather它應該接受一個數字參數,我們將調用它temperature(但你可以隨意命名)。如果temperature大于或等于 75,則返回true。否則,返回false本練習假設temperature溫度為華氏度預期結果:isShortsWeather(80) //trueisShortsWeather(48) //falseisShortsWeather(75) //true我寫的代碼是:function isShortsWeather(temperature) {    if (temperature < 75); {        return false;    }     if (temperature >= 75) {        return true;    }}作為片段:function isShortsWeather(temperature) {    if (temperature < 75); {        return false;    }     if (temperature >= 75) {        return true;    }}console.log(isShortsWeather(80)) //trueconsole.log(isShortsWeather(48)) //falseconsole.log(isShortsWeather(75)) //true請幫助我,告訴我我的代碼有什么問題以及我應該如何解決這個問題。我覺得我比較接近解決它。謝謝!
查看完整描述

5 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

它不起作用的主要原因是因為你有一個額外的; 在第一個條件之后。您可以將函數體縮短為一行

function isShortsWeather(temperature) {
    return temperature >= 75;
}


查看完整回答
反對 回復 2023-06-15
?
catspeake

TA貢獻1111條經驗 獲得超0個贊

只需返回布爾值:

return temperature >= 75;


查看完整回答
反對 回復 2023-06-15
?
慕森王

TA貢獻1777條經驗 獲得超3個贊

我建議在兩個 if 語句上返回,而不是在 false 上返回控制臺日志。您將使用 console.log 來調用該函數。我對代碼進行了一些編輯,因為不需要第 2 行的分號。


function isShortsWeather(temperature) {

  if (temperature < 75) {

    return false;

  } else {

    return true;

  }

}


temperature = 74;

console.log(isShortsWeather(temperature));


查看完整回答
反對 回復 2023-06-15
?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

你忘了;在第 2 行,如果你刪除它它就會工作。如果你在中進行第二個 if 語句也會更好else if


    function isShortsWeather(temperature) {

        if (temperature < 75) {

            return false;

        } else if (temperature >= 75) {

            return true;

    }


查看完整回答
反對 回復 2023-06-15
?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

您將在下面找到有效的正確代碼。希望回答你的問題。


function isShortsWeather(temperature) {

    if (temperature >= 75) {

        return true;

    } 

        else {

        return false;

    }

}


console.log(isShortsWeather(76));

console.log(isShortsWeather(74));


查看完整回答
反對 回復 2023-06-15
  • 5 回答
  • 0 關注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號