用HTML、CSS和JavaScript制作的计算器。我不知道哪里出了问题。

huangapple go评论117阅读模式
英文:

Calculator with html, css and js. I have no idea what's wrong

问题

我理解你的情况,让我来看一下你的代码并尝试找出问题。

从你的代码来看,有一些地方可能会导致计算器工作不正常。首先,请检查以下几点:

  1. 确保你的HTML文件中正确引用了JavaScript文件。你的HTML文件中有一个script标签,但是文件名似乎有一个拼写错误。应该是 <script src="script.js"></script>,而不是 <script src="scrip.js"></script>

  2. 在HTML中,你使用了一些HTML实体编码(如 &larr;&times;&plus; 等)。请确保这些编码都正确,以防止HTML渲染问题。

  3. 在JavaScript中,你使用了 handleMath 函数来处理加法、减法、乘法和除法,但在代码中检查操作符的时候,操作符应该是 '+''-''×''÷',而不是 '+', '−', '×', '÷'。确保操作符的字符串值与你的switch语句中的情况匹配。

  4. 请确保你的事件监听器绑定正确,以确保在按钮被点击时 buttonClick 函数被正确调用。

检查这些问题,并尝试进行修复。如果问题仍然存在,请提供更多信息,以便我可以进一步帮助你诊断问题。

英文:

Followed a youtube tutorial, reviewed the code countless times and I don't know what's wrong. The calculator sometimes works, other times doesnt. Some buttons (+, -, x, ⁄) dont work. Here's the entire code cause I have no ideia where the problem might be. I started with html, css and then js, I think the problem must be in the js part

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. let runningTotal = 0;
  2. let buffer = 0;
  3. let previousOperator;
  4. const screen = document.querySelector(&#39;.screen&#39;);
  5. function buttonClick(value){
  6. if (isNaN(value)){
  7. handleSymbol(value);
  8. }else{
  9. handleNumber(value);
  10. }
  11. screen.innerText = buffer;
  12. }
  13. function handleSymbol(symbol){
  14. switch(symbol){
  15. case &quot;C&quot;:
  16. buffer = &#39;0&#39;;
  17. runningTotal = 0;
  18. break;
  19. case &#39;=&#39;:
  20. if(previousOperator === null){
  21. return
  22. }
  23. flushOperation (parseInt(buffer));
  24. previousOperator = null;
  25. buffer = runningTotal;
  26. runningTotal = 0;
  27. break
  28. case &#39;←&#39;:
  29. if(buffer.length === 1){
  30. buffer = &#39;0&#39;;
  31. }else{
  32. buffer = buffer.substring(0, buffer.length - 1);
  33. }
  34. break;
  35. case &#39;+&#39;:
  36. case &#39;−&#39;:
  37. case &#39;&#215;&#39;:
  38. case &#39;&#247;&#39;:
  39. handleMath(symbol);
  40. break;
  41. }
  42. }
  43. function handleMath(symbol){
  44. if(buffer === &#39;0&#39;){
  45. return;
  46. }
  47. const intBuffer = parseInt(buffer);
  48. if(runningTotal === &#39;0&#39;){
  49. runningTotal = intBuffer
  50. }else{
  51. flushOperation(intBuffer);
  52. }
  53. previousOperator = symbol;
  54. buffer = &#39;0&#39;;
  55. }
  56. function flushOperation(intBuffer){
  57. if(previousOperator === &#39;+&#39;){
  58. runningTotal += intBuffer;
  59. }else if(previousOperator === &#39;−&#39;){
  60. runningTotal -= intBuffer;
  61. }else if(previousOperator === &#39;&#215;&#39;){
  62. runningTotal *= intBuffer;
  63. }else if(previousOperator === &#39;&#247;&#39;){
  64. runningTotal /= intBuffer;
  65. }
  66. }
  67. function handleNumber(numberString){
  68. if(buffer === &#39;0&#39;){
  69. buffer = numberString;
  70. }else{
  71. buffer += numberString;
  72. }
  73. }
  74. function init(){
  75. document.querySelector(&#39;.calc-buttons&#39;).addEventListener(&#39;click&#39;, function(event){
  76. buttonClick(event.target.innerText);
  77. })
  78. }
  79. init();

<!-- language: lang-css -->

  1. html{
  2. box-sizing: border-box;
  3. height: 100%;
  4. }
  5. *,
  6. *::before,
  7. *::after{
  8. box-sizing: inherit;
  9. margin: 0;
  10. padding: 0;
  11. }
  12. body{
  13. align-items: center;
  14. background: linear-gradient(320deg, #eb92be, #ffef78, #63c9b4);
  15. display: flex;
  16. font-family: &#39;Dosis&#39;, sans-serif;
  17. font-display: swap;
  18. height: inherit;
  19. justify-content: center;
  20. }
  21. .wrapper{
  22. backdrop-filter: blur(5.5px);
  23. -webkit-backdrop-filter: blur(55.5px);
  24. border-radius: 16px;
  25. box-shadow: 0 4px 30px rgba(35, 35, 35, 0.1);
  26. color: #232323;
  27. backdrop-filter: blur(4px);
  28. -webkit-backdrop-filter: blur(4px);
  29. background: rgba(225, 225, 225, 0.30);
  30. border: 1px solid rgba(225, 225, 225, 0.34);
  31. flex-basis: 400px;
  32. height: 540px;
  33. padding: 20px 35px;
  34. }
  35. .screen{
  36. backdrop-filter: blur(5.5px);
  37. -webkit-backdrop-filter: blur(5.5px);
  38. background:rgba(225, 225, 225, 0.75);
  39. border: 1px solid rgba(225, 225, 225, 0.01);
  40. border-radius: 16px;
  41. box-shadow: 0 4px 30px rgba(35, 35, 35, 0.1);
  42. color: #232323;
  43. font-size: 35px;
  44. overflow: auto;
  45. padding: 20px;
  46. text-align: right;
  47. width: 326px;
  48. }
  49. .calc-button-row{
  50. display: flex;
  51. justify-content: space-between;
  52. margin: 5% 0;
  53. }
  54. .calc-button{
  55. backdrop-filter: blur(5.5px);
  56. -webkit-backdrop-filter: blur(5.5px);
  57. background: rgba(225, 225, 225, 0.75);
  58. border: 1px solid rgba(255, 255, 255, 0.01);
  59. border-radius: 16px;
  60. box-shadow: 0 4px 30px rgba(35, 35, 35, 0.1);
  61. color: #232323;
  62. flex-basis: 20%;
  63. font-family: inherit;
  64. font-size: 24px;
  65. height: 65px;
  66. }
  67. .calc-button:last-child{
  68. backdrop-filter: blur(5.5px);
  69. -webkit-backdrop-filter: blur(5.5px);
  70. background: rgba(225, 225, 225, 0.75);
  71. border: 1px solid rgba(255, 255, 255, 0.01);
  72. border-radius: 16px;
  73. box-shadow: 0 4px 30px rgba(35, 35, 35, 0.1);
  74. color: #fff;
  75. background: #d72880;
  76. }
  77. .calc-button:last-child:hover{
  78. background-color: inherit;
  79. color: inherit;
  80. }
  81. .calc-button:hover{
  82. background-color: inherit;
  83. }
  84. .calc-button:active{
  85. background-color: #ffef78;
  86. }
  87. .double{
  88. flex-basis: 47%;
  89. }
  90. .triple{
  91. flex-basis: 73%;
  92. }

<!-- language: lang-html -->

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  6. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  7. &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
  8. &lt;title&gt;Calculator&lt;/title&gt;
  9. &lt;/head&gt;
  10. &lt;body&gt;
  11. &lt;div class=&quot;wrapper&quot;&gt;
  12. &lt;section class=&quot;screen&quot;&gt;
  13. 0
  14. &lt;/section&gt;
  15. &lt;section class=&quot;calc-buttons&quot;&gt;
  16. &lt;div class=&quot;calc-button-row&quot;&gt;
  17. &lt;button class=&quot;calc-button double&quot;&gt;
  18. C
  19. &lt;/button&gt;
  20. &lt;button class=&quot;calc-button&quot;&gt;
  21. &amp;larr;
  22. &lt;/button&gt;
  23. &lt;button class=&quot;calc-button&quot;&gt;
  24. &amp;divide;
  25. &lt;/button&gt;
  26. &lt;/div&gt;
  27. &lt;div class=&quot;calc-button-row&quot;&gt;
  28. &lt;button class=&quot;calc-button&quot;&gt;
  29. 7
  30. &lt;/button&gt;
  31. &lt;button class=&quot;calc-button&quot;&gt;
  32. 8
  33. &lt;/button&gt;
  34. &lt;button class=&quot;calc-button&quot;&gt;
  35. 9
  36. &lt;/button&gt;
  37. &lt;button class=&quot;calc-button&quot;&gt;
  38. &amp;times;
  39. &lt;/button&gt;
  40. &lt;/div&gt;
  41. &lt;div class=&quot;calc-button-row&quot;&gt;
  42. &lt;button class=&quot;calc-button&quot;&gt;
  43. 4
  44. &lt;/button&gt;
  45. &lt;button class=&quot;calc-button&quot;&gt;
  46. 5
  47. &lt;/button&gt;
  48. &lt;button class=&quot;calc-button&quot;&gt;
  49. 6
  50. &lt;/button&gt;
  51. &lt;button class=&quot;calc-button&quot;&gt;
  52. &amp;minus;
  53. &lt;/button&gt;
  54. &lt;/div&gt;
  55. &lt;div class=&quot;calc-button-row&quot;&gt;
  56. &lt;button class=&quot;calc-button&quot;&gt;
  57. 1
  58. &lt;/button&gt;
  59. &lt;button class=&quot;calc-button&quot;&gt;
  60. 2
  61. &lt;/button&gt;
  62. &lt;button class=&quot;calc-button&quot;&gt;
  63. 3
  64. &lt;/button&gt;
  65. &lt;button class=&quot;calc-button&quot;&gt;
  66. &amp;plus;
  67. &lt;/button&gt;
  68. &lt;/div&gt;
  69. &lt;div class=&quot;calc-button-row&quot;&gt;
  70. &lt;button class=&quot;calc-button triple&quot;&gt;
  71. 0
  72. &lt;/button&gt;
  73. &lt;button class=&quot;calc-button&quot;&gt;
  74. &amp;equals;
  75. &lt;/button&gt;
  76. &lt;/div&gt;
  77. &lt;/section&gt;
  78. &lt;/div&gt;
  79. &lt;script src=&quot;scrip.js&quot;&gt;&lt;/script&gt;
  80. &lt;/body&gt;
  81. &lt;/html&gt;

<!-- end snippet -->

I've reviewed the code word by word, can't find the problem

答案1

得分: 1

  1. 有一些问题我找到了:

  2. 有时缓冲区是数字,有时是字符串。所以在这一行中:

    buffer = buffer.substring(0, buffer.length - 1);

    你会遇到未捕获的类型错误,所以尽量将它转换为字符串:

    buffer = buffer.toString().substring(0, buffer.length - 1);

  3. 你试图检查 runningTotal 是否等于字符串 '0',但它是一个数字。

    if (runningTotal === 0)

    你需要仔细检查所有变量的类型,因为现在有点混乱。

英文:

There are couple of issues I found:

  1. Sometimes buffer is number, sometimes - string. So in line:

buffer = buffer.substring(0, buffer.length - 1);

you have an Uncaught TypeError, so try always convert it to a string:

buffer = buffer.toString().substring(0, buffer.length - 1);

  1. You are trying to check that runningTotal equals string '0', but it is a number.

if (runningTotal === 0)

You need to carefully check the types of all variables because now it's a bit messy.

huangapple
  • 本文由 发表于 2023年5月21日 01:11:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定