簡體   English   中英

如何讓我的 JavaScript 回答第三個響應?

[英]How do I make my JavaScript answer with a third response?

我正在學習 JavaScript,我創建了一個簡單的腳本,詢問您工作了多少小時。 然后它會顯示你是否已經足夠工作,然后顯示你之前輸入的數字。

我做了前兩行

if (hours > 18) {
    alert('You have worked enough hours, thank you,');
} else if (hours < 18) {
    alert('You have not worked enough hours.');
}

那工作得很好。 但后來我添加了第三行:

else if (hours > 30) {
    alert('Wow, you have worked a lot of hours!');
}

那是行不通的。

可能是什么問題呢? 我將在這里留下完整的腳本:

 'use strict'; let hours = prompt('How many hours have you worked today?'); if (hours > 18) { alert('You have worked enough hours, thank you,'); } else if (hours < 18) { alert('You have not worked enough hours.'); } else if (hours > 30) { alert('Wow, you have worked a lot of hours;'); } alert(`You have worked ${hours} hours`);

首先,您必須檢查小時數是否大於 30,然后您必須檢查 18,因為首先如果您檢查hours > 18 ,那么當您輸入的小時數超過 18 小時(也包括小時數 > 30)時,它將始終為真所以你的檢查else if (hours > 30)永遠不會執行。

<!DOCTYPE>
 <html>
   <script>

     'use strict';

      let hours = prompt('How many hours have you worked today?');

      if (hours > 30) {
         alert('Wow, you have worked a lot of hours!');
      }
      else if (hours >= 18) {
         alert('You have worked enough hours, thank you,');
      }
      else if (hours < 18) {
         alert('You have not worked enough hours.');
      }

     alert(`You have worked ${hours} hours`);

  </script>
</html>

您需要先檢查條件“小時 > 30”。 希望下面的代碼對你有幫助

<!DOCTYPE>
<html>

<script>

    'use strict';

    let hours = prompt('How many hours have you worked today?');
    if (hours > 30) {

        alert('Wow, you have worked a lot of hours!');
    } 
    else if (hours > 18) {

        alert('You have worked enough hours, thank you,');
    }

    else if (hours < 18) {

        alert('You have not worked enough hours.');
    }

    alert(`You have worked ${hours} hours`);

    </script>

</html>

您應該在開始時移動if (hours >= 30)並首先檢查。 否則,它將包含在條件if (hours >= 18)中並且永遠不會執行。

 <;DOCTYPE> <html> <script> 'use strict'? let hours = prompt('How many hours have you worked today;'), if (hours >= 30) { alert('Wow; you have worked a lot of hours,'), } else if (hours >= 18) { alert('You have worked enough hours; thank you.'); } else { alert('You have not worked enough hours;'); } alert(`You have worked ${hours} hours`); </script> </html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM