首先练习数字相关的一些操作:

  1. <div>
  2. <label>Number A:<input id="radio-a" type="radio" name="math-obj" value="a"></label><input id="num-a" type="text">
  3. <label>Number B:<input id="radio-b" type="radio" name="math-obj" value="b"></label><input id="num-b" type="text">
  4. </div>
  5. <div>
  6. <button>判断当前选中的输入框输入内容是否为数字</button>
  7. <button>把 A 四舍五入为 B 个小数位数的数字</button>
  8. <button>当前选中数字的绝对值</button>
  9. <button>对当前选中的数字进行上舍入</button>
  10. <button>对当前选中的数字进行下舍入</button>
  11. <button>把当前选中的数字四舍五入为最接近的整数</button>
  12. <button>返回 A 和 B 中的最高值</button>
  13. <button>返回 A 和 B 中的最低值</button>
  14. </div>
  15. <p id="result"></p>

基于如上HTML,实现需求

  • 按照HTML中按钮的描述以此实现功能
  • 计算结果显示在 id 为 result 的 P 标签中
  • 除了第一个按钮,其它按钮操作时,都需要判断输入是否为数字,否则在 console 中输出错误信息
  1. 1 <!DOCTYPE html>
  2. 2 <html>
  3. 3
  4. 4 <head>
  5. 5 <meta charset="utf-8" />
  6. 6 <title>JS里的居民们1</title>
  7. 7
  8. 8 </head>
  9. 9
  10. 10 <body>
  11. 11 <div>
  12. 12 <label>Number A:<input id="radio-a" type="radio" name="math-obj" value="a"></label><input id="num-a" type="text">
  13. 13 <label>Number B:<input id="radio-b" type="radio" name="math-obj" value="b"></label><input id="num-b" type="text">
  14. 14 </div>
  15. 15 <div>
  16. 16 <button>判断当前选中的输入框输入内容是否为数字</button>
  17. 17 <button> A 四舍五入为 B 个小数位数的数字</button>
  18. 18 <button>当前选中数字的绝对值</button>
  19. 19 <button>对当前选中的数字进行上舍入</button>
  20. 20 <button>对当前选中的数字进行下舍入</button>
  21. 21 <button>把当前选中的数字四舍五入为最接近的整数</button>
  22. 22 <button>返回 A B 中的最高值</button>
  23. 23 <button>返回 A B 中的最低值</button>
  24. 24 </div>
  25. 25 <p id="result"></p>
  26. 26
  27. 27 <script>
  28. 28 //定义常量
  29. 29 const numA = document.getElementById("num-a");
  30. 30 numB = document.getElementById("num-b");
  31. 31 radioA = document.getElementById("radio-a");
  32. 32 radioB = document.getElementById("radio-b");
  33. 33 buttons = document.getElementsByTagName("button"); //按钮数组
  34. 34 p = document.getElementById("result");
  35. 35 //定义数字检查
  36. 36 function isNumber(num) {
  37. 37 if (!isNaN(num.value) && num.value != "") {
  38. 38 return true;
  39. 39 } else {
  40. 40 return false;
  41. 41 }
  42. 42 }
  43. 43 //定义勾选检查
  44. 44 function isCheck(check) {
  45. 45 if (check.checked) {
  46. 46 return true;
  47. 47 } else {
  48. 48 return false;
  49. 49 }
  50. 50 }
  51. 51 //按钮1监听点击,实现判断当前选中的输入框输入内容是否为数字
  52. 52 buttons[0].addEventListener("click", function () {
  53. 53 if (isCheck(radioA)) {
  54. 54 if (isNumber(numA)) {
  55. 55 p.innerHTML = "这是一个数字";
  56. 56 } else {
  57. 57 p.innerHTML = "这不是一个数字";
  58. 58 console.log("这不是一个数字哈!");
  59. 59 }
  60. 60 }
  61. 61 if (isCheck(radioB)) {
  62. 62 if (isNumber(numB)) {
  63. 63 p.innerHTML = "这是一个数字";
  64. 64 } else {
  65. 65 p.innerHTML = "这不是一个数字";
  66. 66 console.log("这不是一个数字哈!");
  67. 67 }
  68. 68 }
  69. 69 })
  70. 70 //按钮2监听点击,实现把A四舍五入为B个小数位数的数字
  71. 71 buttons[1].addEventListener("click", function () {
  72. 72 if (isNumber(numA) && isNumber(numB)) {
  73. 73 var A = parseFloat(numA.value);
  74. 74 p.innerHTML = A.toFixed(parseInt(numB.value));
  75. 75 } else {
  76. 76 console.log("必须均为数字哦!")
  77. 77 }
  78. 78 })
  79. 79 //按钮3监听点击,实现当前选中数字的绝对值
  80. 80 buttons[2].addEventListener("click", function () {
  81. 81 if (isCheck(radioA)) {
  82. 82 if (isNumber(numA)) {
  83. 83 p.innerHTML = Math.abs(Number(numA.value));
  84. 84 } else {
  85. 85 console.log("非数字!");
  86. 86 }
  87. 87 }
  88. 88 if (isCheck(radioB)) {
  89. 89 if (isNumber(numB)) {
  90. 90 p.innerHTML = Math.abs(Number(numB.value));
  91. 91 } else {
  92. 92 console.log("非数字!");
  93. 93 }
  94. 94 }
  95. 95 })
  96. 96 //按钮4监听点击,实现对当前选中的数字进行上舍入
  97. 97 buttons[3].addEventListener("click", function () {
  98. 98 if (isCheck(radioA)) {
  99. 99 p.innerHTML = Math.ceil(Number(numA.value));
  100. 100 }
  101. 101 if (isCheck(radioB)) {
  102. 102 p.innerHTML = Math.ceil(Number(numB.value));
  103. 103 }
  104. 104 })
  105. 105 //按钮5监听点击,实现对当前选中的数字进行下舍入
  106. 106 buttons[4].addEventListener("click", function () {
  107. 107 if (isCheck(radioA)) {
  108. 108 p.innerHTML = Math.floor(Number(numA.value));
  109. 109 }
  110. 110 if (isCheck(radioB)) {
  111. 111 p.innerHTML = Math.floor(Number(numB.value));
  112. 112 }
  113. 113 })
  114. 114 //按钮6监听点击,实现把当前选中的数字四舍五入为最接近的整数
  115. 115 buttons[5].addEventListener("click", function () {
  116. 116 if (isCheck(radioA)) {
  117. 117 p.innerHTML = Math.round(Number(numA.value));
  118. 118 }
  119. 119 if (isCheck(radioB)) {
  120. 120 p.innerHTML = Math.round(Number(numB.value));
  121. 121 }
  122. 122 })
  123. 123 //按钮7监听点击,实现返回A和B中的最高值
  124. 124 buttons[6].addEventListener("click", function () {
  125. 125 p.innerHTML = Math.max(Number(numA.value), Number(numB.value));
  126. 126 })
  127. 127 //按钮8监听点击,实现返回A和B中的最小值
  128. 128 buttons[7].addEventListener("click", function () {
  129. 129 p.innerHTML = Math.min(Number(numA.value), Number(numB.value));
  130. 130 })
  131. 131 </script>
  132. 132 </body>
  133. 133
  134. 134 </html>

注意点:Math.round()判断有小数点大于等于0.5的,向上取整数。比如Math.round(-1.5)=-1;Math.round(1.5)=2

版权声明:本文为Joe-and-Joan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Joe-and-Joan/p/10065492.html