跟著一個(gè)youtube教程,看了無數(shù)遍代碼也不知道哪里出了問題。計(jì)算器有時(shí)工作,有時(shí)不工作。有些按鈕(+、-、x)不起作用。這是完整的代碼,因?yàn)槲也恢绬栴}可能出在哪里。我先從html,css開始,然后是js,我覺得問題一定在js部分
let runningTotal = 0;
let buffer = 0;
let previousOperator;
const screen = document.querySelector('.screen');
function buttonClick(value){
if (isNaN(value)){
handleSymbol(value);
}else{
handleNumber(value);
}
screen.innerText = buffer;
}
function handleSymbol(symbol){
switch(symbol){
case "C":
buffer = '0';
runningTotal = 0;
break;
case '=':
if(previousOperator === null){
return
}
flushOperation (parseInt(buffer));
previousOperator = null;
buffer = runningTotal;
runningTotal = 0;
break
case '←':
if(buffer.length === 1){
buffer = '0';
}else{
buffer = buffer.substring(0, buffer.length - 1);
}
break;
case '+':
case '?':
case '×':
case '÷':
handleMath(symbol);
break;
}
}
function handleMath(symbol){
if(buffer === '0'){
return;
}
const intBuffer = parseInt(buffer);
if(runningTotal === '0'){
runningTotal = intBuffer
}else{
flushOperation(intBuffer);
}
previousOperator = symbol;
buffer = '0';
}
function flushOperation(intBuffer){
if(previousOperator === '+'){
runningTotal += intBuffer;
}else if(previousOperator === '?'){
runningTotal -= intBuffer;
}else if(previousOperator === '×'){
runningTotal *= intBuffer;
}else if(previousOperator === '÷'){
runningTotal /= intBuffer;
}
}
function handleNumber(numberString){
if(buffer === '0'){
buffer = numberString;
}else{
buffer += numberString;
}
}
function init(){
document.querySelector('.calc-buttons').addEventListener('click', function(event){
buttonClick(event.target.innerText);
})
}
init();
html{
box-sizing: border-box;
height: 100%;
}
*,
*::before,
*::after{
box-sizing: inherit;
margin: 0;
padding: 0;
}
body{
align-items: center;
background: linear-gradient(320deg, #eb92be, #ffef78, #63c9b4);
display: flex;
font-family: 'Dosis', sans-serif;
font-display: swap;
height: inherit;
justify-content: center;
}
.wrapper{
backdrop-filter: blur(5.5px);
-webkit-backdrop-filter: blur(55.5px);
border-radius: 16px;
box-shadow: 0 4px 30px rgba(35, 35, 35, 0.1);
color: #232323;
backdrop-filter: blur(4px);
-webkit-backdrop-filter: blur(4px);
background: rgba(225, 225, 225, 0.30);
border: 1px solid rgba(225, 225, 225, 0.34);
flex-basis: 400px;
height: 540px;
padding: 20px 35px;
}
.screen{
backdrop-filter: blur(5.5px);
-webkit-backdrop-filter: blur(5.5px);
background:rgba(225, 225, 225, 0.75);
border: 1px solid rgba(225, 225, 225, 0.01);
border-radius: 16px;
box-shadow: 0 4px 30px rgba(35, 35, 35, 0.1);
color: #232323;
font-size: 35px;
overflow: auto;
padding: 20px;
text-align: right;
width: 326px;
}
.calc-button-row{
display: flex;
justify-content: space-between;
margin: 5% 0;
}
.calc-button{
backdrop-filter: blur(5.5px);
-webkit-backdrop-filter: blur(5.5px);
background: rgba(225, 225, 225, 0.75);
border: 1px solid rgba(255, 255, 255, 0.01);
border-radius: 16px;
box-shadow: 0 4px 30px rgba(35, 35, 35, 0.1);
color: #232323;
flex-basis: 20%;
font-family: inherit;
font-size: 24px;
height: 65px;
}
.calc-button:last-child{
backdrop-filter: blur(5.5px);
-webkit-backdrop-filter: blur(5.5px);
background: rgba(225, 225, 225, 0.75);
border: 1px solid rgba(255, 255, 255, 0.01);
border-radius: 16px;
box-shadow: 0 4px 30px rgba(35, 35, 35, 0.1);
color: #fff;
background: #d72880;
}
.calc-button:last-child:hover{
background-color: inherit;
color: inherit;
}
.calc-button:hover{
background-color: inherit;
}
.calc-button:active{
background-color: #ffef78;
}
.double{
flex-basis: 47%;
}
.triple{
flex-basis: 73%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Calculator</title>
</head>
<body>
<div class="wrapper">
<section class="screen">
0
</section>
<section class="calc-buttons">
<div class="calc-button-row">
<button class="calc-button double">
C
</button>
<button class="calc-button">
←
</button>
<button class="calc-button">
÷
</button>
</div>
<div class="calc-button-row">
<button class="calc-button">
7
</button>
<button class="calc-button">
8
</button>
<button class="calc-button">
9
</button>
<button class="calc-button">
×
</button>
</div>
<div class="calc-button-row">
<button class="calc-button">
4
</button>
<button class="calc-button">
5
</button>
<button class="calc-button">
6
</button>
<button class="calc-button">
−
</button>
</div>
<div class="calc-button-row">
<button class="calc-button">
1
</button>
<button class="calc-button">
2
</button>
<button class="calc-button">
3
</button>
<button class="calc-button">
+
</button>
</div>
<div class="calc-button-row">
<button class="calc-button triple">
0
</button>
<button class="calc-button">
=
</button>
</div>
</section>
</div>
<script src="scrip.js"></script>
</body>
</html>
我發(fā)現(xiàn)了幾個(gè)問題:
有時(shí)緩沖區(qū)是數(shù)字,有時(shí)是字符串。所以排隊(duì): buffer = buffer.substring(0,buffer . length-1);
您有一個(gè)未捕獲的TypeError,所以嘗試總是將其轉(zhuǎn)換為字符串:
buffer = buffer.toString()。substring(0,buffer . length-1);
您正在嘗試檢查runningTotal是否等于字符串“0”,但它是一個(gè)數(shù)字。 if (runningTotal === 0)
你需要仔細(xì)檢查所有變量的類型,因?yàn)楝F(xiàn)在有點(diǎn)亂。