- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我找不到让这个函数 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">√</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="/">÷</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="*">×</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">√</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="/">÷</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="*">×</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/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!