gpt4 book ai didi

JavaScript 计算器 - 一个按钮不起作用(平方根)

转载 作者:行者123 更新时间:2023-11-30 06:20:26 26 4
gpt4 key购买 nike

我找不到让这个函数 Math.sqrt(value) 工作的方法。 (╯°□°)╯︵┻━┻我不确定,但问题似乎出在运营商。我还尝试在 const calculation 中添加该函数,但它也不起作用。

我不想在 HTML 中设置 onclick 来调用函数。我试图在整个代码中保持相同的逻辑。但是我不知道如何解决这个问题。

Ps .: 我建议复制文件并在 Safari 或 Firefox 中测试。计算器看起来不错。我试图创建一个与 macOS 计算器  非常相似的版本。

const calculator = {
displayValue: "0",
firstOperand: null,
waitSecondOperand: false,
operator: null
};

function inputDigit(digit) {
const { displayValue, waitSecondOperand } = calculator;

if (waitSecondOperand === true) {
calculator.displayValue = digit;
calculator.waitSecondOperand = false;
} else {
calculator.displayValue =
displayValue === "0" ? digit : displayValue + digit;
}

console.log(calculator);
}

function decimal(dot) {
if (calculator.waitSecondOperand === true) return;

if (!calculator.displayValue.includes(dot)) {
calculator.displayValue += dot;
}
}

function inputDigit(digit) {
const { displayValue, waitSecondOperand } = calculator;

if (waitSecondOperand === true) {
calculator.displayValue = digit;
calculator.waitSecondOperand = false;
} else {
calculator.displayValue =
displayValue === "0" ? digit : displayValue + digit;
}
console.log(calculator);
}

function operators(nextOperator) {
const { firstOperand, displayValue, operator } = calculator;
const inputValue = parseFloat(displayValue);

if (operator && calculator.waitSecondOperand) {
calculator.operator = nextOperator;
console.log(calculator);
return;
}

if (firstOperand == null) {
calculator.firstOperand = inputValue;
} else if (operator) {
const currentValue = firstOperand || 0;
const result = calculation[operator](currentValue, inputValue);

calculator.displayValue = String(result);
calculator.firstOperand = result;
}

calculator.waitSecondOperand = true;
calculator.operator = nextOperator;
console.log(calculator);
}

const calculation = {

"%": (firstOperand, secondOperand) => (firstOperand * secondOperand) / 100,

"/": (firstOperand, secondOperand) => firstOperand / secondOperand,

"*": (firstOperand, secondOperand) => firstOperand * secondOperand,

"+": (firstOperand, secondOperand) => firstOperand + secondOperand,

"-": (firstOperand, secondOperand) => firstOperand - secondOperand,

"=": (firstOperand, secondOperand) => secondOperand
};

// I can not figure out how to make this function work.
// Below the source of my despair.

const getSqrt = document.querySelector(".sqrt");
getSqrt.addEventListener("click", firstOperand => {
calculator.displayValue = Math.sqrt(firstOperand);
});

function resetCalculator() {
calculator.displayValue = "0";
calculator.firstOperand = null;
calculator.waitingForSecondOperand = false;
calculator.operator = null;
console.log(calculator);
}

function updateDisplay() {
const display = document.querySelector(".calculator--display");
display.value = calculator.displayValue;
}

updateDisplay();


const buttons = document.querySelector(".calculator--keys");
buttons.addEventListener("click", e => {

const { target } = e;
if (!target.matches("button")) {
return;
}
if (target.classList.contains("operator")) {
operators(target.value);
updateDisplay();
return;
}
if (target.classList.contains("decimal")) {
decimal(target.value);
updateDisplay();
return;
}
if (target.classList.contains("clear")) {
resetCalculator();
updateDisplay();
return;
}
inputDigit(target.value);
updateDisplay();
});
@import url("https://fonts.googleapis.com/css?family=Jura:500");
*,
*:after,
*:before {
margin: 0px;
padding: 0px;
border-spacing: 0;
border-collapse: collapse;
box-sizing: border-box;
border: none;
box-sizing: border-box;
}
html {
height: 100%;
background: #655f5c;
box-sizing: border-box;
}
.container {
width: 375px;
height: 564px;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
button,
input {
color: #fff;
font-family: "Jura", sans-serif;
font-weight: 500;
}
.calculator {
background: #312f2e;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
}
.calculator--keys {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.calculator--keys > tr {
display: flex;
flex-wrap: wrap;
width: 100%;
}
.calculator--keys tr > td {
width: 25%;
}
table tr:first-child button {
background-color: #605c57;
}
.calculator--display,
.calculator--keys__button {
color: #fff;
padding: 10px;
}
.calculator--display {
background-color: #312f2e;
border: none;
color: #fff;
height: 95.2px;
width: 100%;
font-size: 1.8em;
text-align: right;
}
.calculator--keys__button {
display: flex;
border: solid #312f2e;
border-width: 0.01em;
cursor: pointer;
font-size: 1.5em;
width: 100%;
height: 93.75px;
background-color: #8a8783;
justify-content: center;
align-items: center;
text-align: center;
transition: all 0.03s;
}
.calculator--keys__button:focus,
.calculator--keys__button:active {
outline: none;
}
.press {
transition: all 0.03s;
transform: scale(0.98);
}
.zero {
-webkit-flex-grow: 2;
flex-grow: 2;
}
.orange {
font-size: 2em;
background-color: #ff9f0c !important;
}
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="./css/styles.css" />
<title>Calculadora</title>
</head>
<body>
<section>
<div class="container">
<div class="calculator flex">
<input
type="text"
class="calculator--display"
name="calculator--display"
value=""
disabled
/>
<table>
<tbody class="calculator--keys">
<tr>
<td><button type="button" class="calculator--keys__button key clear" value="clear">C</button></td>
<td><button type="button" class="calculator--keys__button operator sqrt" value="sqrt">&radic;</button></td>
<td><button type="button" class="calculator--keys__button operator perc" value="%">%</button></td>
<td><button type="button" class="calculator--keys__button operator orange" value="/">&divide;</button></td>
</tr>
<tr>
<td><button type="button" class="calculator--keys__button" value="7">7</button></td>
<td><button type="button" class="calculator--keys__button" value="8">8</button></td>
<td><button type="button" class="calculator--keys__button" value="9">9</button></td>
<td><button type="button" class="calculator--keys__button operator orange" value="*">&times;</button></td>
</tr>
<tr>
<td><button type="button" class="calculator--keys__button" value="4">4</button></td>
<td><button type="button" class="calculator--keys__button" value="5">5</button></td>
<td><button type="button" class="calculator--keys__button" value="6">6</button></td>
<td><button type="button" class="calculator--keys__button operator orange" value="-">-</button></td>
</tr>
<tr>
<td><button type="button" class="calculator--keys__button" value="1">1</button></td>
<td><button type="button" class="calculator--keys__button" value="2">2</button></td>
<td><button type="button" class="calculator--keys__button" value="3">3</button></td>
<td><button type="button" class="calculator--keys__button operator orange" value="+">+</button></td>
</tr>
<tr>
<td class="zero"><button type="button" class="calculator--keys__button" value="0">0</button></td>
<td><button type="button" class="calculator--keys__button decimal" value=".">.</button></td>
<td><button type="button" class="calculator--keys__button equal operator orange" value="=">=</button></td>
</tr>
</tbody>
</table>
</div>
</section>
<script src="./js/build.js"></script>
</body>
</html>

我每天都在努力开发一些东西,以成为更好的开发者。非常感谢您的帮助。

最佳答案

const calculator = {
displayValue: "0",
firstOperand: null,
waitSecondOperand: false,
operator: null
};

function inputDigit(digit) {
const { displayValue, waitSecondOperand } = calculator;

if (waitSecondOperand === true) {
calculator.displayValue = digit;
calculator.waitSecondOperand = false;
} else {
calculator.displayValue =
displayValue === "0" ? digit : displayValue + digit;
}

console.log(calculator);
}

function decimal(dot) {
if (calculator.waitSecondOperand === true) return;

if (!calculator.displayValue.includes(dot)) {
calculator.displayValue += dot;
}
}

function inputDigit(digit) {
const { displayValue, waitSecondOperand } = calculator;

if (waitSecondOperand === true) {
calculator.displayValue = digit;
calculator.waitSecondOperand = false;
} else {
calculator.displayValue =
displayValue === "0" ? digit : displayValue + digit;
}
console.log(calculator);
}

function operators(nextOperator) {
const { firstOperand, displayValue, operator } = calculator;
const inputValue = parseFloat(displayValue);

if (operator && calculator.waitSecondOperand) {
calculator.operator = nextOperator;
console.log(calculator);
return;
}

if (firstOperand == null) {
calculator.firstOperand = inputValue;
} else if (operator) {
const currentValue = firstOperand || 0;
const result = calculation[operator](currentValue, inputValue);

calculator.displayValue = String(result);
calculator.firstOperand = result;
}

calculator.waitSecondOperand = true;
calculator.operator = nextOperator;
console.log(calculator);
}

const calculation = {

"%": (firstOperand, secondOperand) => (firstOperand * secondOperand) / 100,

"/": (firstOperand, secondOperand) => firstOperand / secondOperand,

"*": (firstOperand, secondOperand) => firstOperand * secondOperand,

"+": (firstOperand, secondOperand) => firstOperand + secondOperand,

"-": (firstOperand, secondOperand) => firstOperand - secondOperand,

"=": (firstOperand, secondOperand) => secondOperand
};

// I can not figure out how to make this function work.
// Below the source of my despair.

const getSqrt = document.querySelector(".sqrt");
getSqrt.addEventListener("click", firstOperand => {
calculator.displayValue = Math.sqrt(calculator.displayValue);
});

function resetCalculator() {
calculator.displayValue = "0";
calculator.firstOperand = null;
calculator.waitingForSecondOperand = false;
calculator.operator = null;
console.log(calculator);
}

function updateDisplay() {
const display = document.querySelector(".calculator--display");
display.value = calculator.displayValue;
}

updateDisplay();


const buttons = document.querySelector(".calculator--keys");
buttons.addEventListener("click", e => {

const { target } = e;
if (!target.matches("button")) {
return;
}
if (target.classList.contains("operator")) {
operators(target.value);
updateDisplay();
return;
}
if (target.classList.contains("decimal")) {
decimal(target.value);
updateDisplay();
return;
}
if (target.classList.contains("clear")) {
resetCalculator();
updateDisplay();
return;
}
inputDigit(target.value);
updateDisplay();
});
@import url("https://fonts.googleapis.com/css?family=Jura:500");
*,
*:after,
*:before {
margin: 0px;
padding: 0px;
border-spacing: 0;
border-collapse: collapse;
box-sizing: border-box;
border: none;
box-sizing: border-box;
}
html {
height: 100%;
background: #655f5c;
box-sizing: border-box;
}
.container {
width: 375px;
height: 564px;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
button,
input {
color: #fff;
font-family: "Jura", sans-serif;
font-weight: 500;
}
.calculator {
background: #312f2e;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
}
.calculator--keys {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.calculator--keys > tr {
display: flex;
flex-wrap: wrap;
width: 100%;
}
.calculator--keys tr > td {
width: 25%;
}
table tr:first-child button {
background-color: #605c57;
}
.calculator--display,
.calculator--keys__button {
color: #fff;
padding: 10px;
}
.calculator--display {
background-color: #312f2e;
border: none;
color: #fff;
height: 95.2px;
width: 100%;
font-size: 1.8em;
text-align: right;
}
.calculator--keys__button {
display: flex;
border: solid #312f2e;
border-width: 0.01em;
cursor: pointer;
font-size: 1.5em;
width: 100%;
height: 93.75px;
background-color: #8a8783;
justify-content: center;
align-items: center;
text-align: center;
transition: all 0.03s;
}
.calculator--keys__button:focus,
.calculator--keys__button:active {
outline: none;
}
.press {
transition: all 0.03s;
transform: scale(0.98);
}
.zero {
-webkit-flex-grow: 2;
flex-grow: 2;
}
.orange {
font-size: 2em;
background-color: #ff9f0c !important;
}
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="./css/styles.css" />
<title>Calculadora</title>
</head>
<body>
<section>
<div class="container">
<div class="calculator flex">
<input
type="text"
class="calculator--display"
name="calculator--display"
value=""
disabled
/>
<table>
<tbody class="calculator--keys">
<tr>
<td><button type="button" class="calculator--keys__button key clear" value="clear">C</button></td>
<td><button type="button" class="calculator--keys__button operator sqrt" value="sqrt">&radic;</button></td>
<td><button type="button" class="calculator--keys__button operator perc" value="%">%</button></td>
<td><button type="button" class="calculator--keys__button operator orange" value="/">&divide;</button></td>
</tr>
<tr>
<td><button type="button" class="calculator--keys__button" value="7">7</button></td>
<td><button type="button" class="calculator--keys__button" value="8">8</button></td>
<td><button type="button" class="calculator--keys__button" value="9">9</button></td>
<td><button type="button" class="calculator--keys__button operator orange" value="*">&times;</button></td>
</tr>
<tr>
<td><button type="button" class="calculator--keys__button" value="4">4</button></td>
<td><button type="button" class="calculator--keys__button" value="5">5</button></td>
<td><button type="button" class="calculator--keys__button" value="6">6</button></td>
<td><button type="button" class="calculator--keys__button operator orange" value="-">-</button></td>
</tr>
<tr>
<td><button type="button" class="calculator--keys__button" value="1">1</button></td>
<td><button type="button" class="calculator--keys__button" value="2">2</button></td>
<td><button type="button" class="calculator--keys__button" value="3">3</button></td>
<td><button type="button" class="calculator--keys__button operator orange" value="+">+</button></td>
</tr>
<tr>
<td class="zero"><button type="button" class="calculator--keys__button" value="0">0</button></td>
<td><button type="button" class="calculator--keys__button decimal" value=".">.</button></td>
<td><button type="button" class="calculator--keys__button equal operator orange" value="=">=</button></td>
</tr>
</tbody>
</table>
</div>
</section>
<script src="./js/build.js"></script>
</body>
</html>

假设您希望人们在点击之前选择一个数字您只需要更改一行的 sqrt 按钮:


calculator.displayValue = Math.sqrt(firstOperand);

calculator.displayValue = Math.sqrt(calculator.displayValue);

在第一种情况下,firstOperand 持有一个鼠标点击事件,它不会作为 Math.sqrt 的参数工作

传入 calculator.displayValue 将作为当前选定的数字工作

关于JavaScript 计算器 - 一个按钮不起作用(平方根),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53481241/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com