计算年终奖

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>计算年终奖</title>
  6. </head>
  7. <body>
  8. 工龄:<input type="text" value="1" id="workYear">
  9. <br>
  10. 薪资:<input type="text" value="14000" id="workMoney">
  11. <br>
  12. <button id="submit">计算年终奖</button>
  13. <br>
  14. 你的年终奖是:¥ <span id="valBox"></span>
  15. <!-- IMPORT JS -->
  16. <script>
  17. let workYear = document.getElementById('workYear');
  18. let workMoney = document.getElementById('workMoney');
  19. let submit = document.getElementById('submit');
  20. let valBox = document.getElementById('valBox');
  21. submit.onclick = function () {
  22. // 1.获取文本框中的信息
  23. // 文本框.value 获取输入的内容,格式是字符串
  24. let year = Number(workYear.value);
  25. let money = Number(workMoney.value);
  26. if (isNaN(year) || isNaN(money)) {
  27. alert('输入的内容是非法格式~~');
  28. return;
  29. }
  30. // 2.经过逻辑处理,计算出结果
  31. let result = null;
  32. if (year < 1) {
  33. result = money >= 8000 ? money * 1.2 : money;
  34. } else if (year >= 1 && year < 2) {
  35. result = money >= 10000 ? money * 1.7 : money * 1.5;
  36. } else {
  37. result = money >= 12000 ? money * 3.2 : money * 3;
  38. }
  39. // 3. 把结果赋值给 #valBox 即可
  40. valBox.innerHTML = result;
  41. }
  42. </script>
  43. </body>
  44. </html>