为什么负数在这个简单的HTML和JavaScript计算器中显示为”5-“而不是”-5″?

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

Why does negative numbers display like "5-" instead of "-5" in this simple calculator on HTML and javaScript?

问题

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>replit</title>
  <style>

#calculator{
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  max-width:400px;
  min-width: 300px;
  background-color: green;
}

#screen{
  width: 100%;  
  aspect-ratio: 4/1.5;
  background-color: rgba(0, 0, 0, 0.8);
}

#input{
  width:90%;
  height:90%;
  background-color:rgba(0, 0, 0, 0);    
  color:white;
  font-size: 15vw;
  direction: rtl;
  font-family: Helvetica, sans-serif;
}

#buttonsGrid{
  width: 100%;
  aspect-ratio: 4/5;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(5, 1fr);
  justify-content: center;
  font-size: 5vw;
  color:white;
  background-color: gray;
  font-family: Helvetica, sans-serif;
}

.singleButton{
  border: solid;
  border-color: black;
  border-width: 1px;
  display: flex;
  align-items: center;
  justify-content: center;

}

[id="0Button"] {
  grid-column: span 2;
}

#equalButton, #plusButton, #minusButton, #xButton, #divisionButton{
 background-color: orange;
} 

#cButton, #negativePositiveButton, #percentageButton {
   background-color: rgba(0, 0, 0, 0.3);
}

</style>

</head>

<body>

  <h1>Simple calculator using JS</h1>

  <div id="calculator">
    <div id="screen">
      <input inputmode="numeric" id="input">
      </input>
    </div>

    <div id="buttonsGrid">
      <div id="cButton" class="singleButton">C
      </div>
      <div id="negativePositiveButton" class="singleButton">+/-
      </div>
      <div id="percentageButton" class="singleButton">%
      </div>
      <div id="divisionButton" class="singleButton"      </div>

      <div id="7Button" class="singleButton numericButton">7</div>
      <div id="8Button" class="singleButton numericButton">8</div>
      <div id="9Button" class="singleButton numericButton">9</div>
      <div id="xButton" class="singleButton">x</div>

      <div id="4Button" class="singleButton numericButton">4</div>
      <div id="5Button" class="singleButton numericButton">5</div>
      <div id="6Button" class="singleButton numericButton">6</div>
      <div id="minusButton" class="singleButton">-</div>

      <div id="1Button" class="singleButton numericButton">1</div>
      <div id="2Button" class="singleButton numericButton">2</div>
      <div id="3Button" class="singleButton numericButton">3</div>
      <div id="plusButton" class="singleButton">+</div>

      <div id="0Button" class="singleButton numericButton">0</div>
      <div id="dotButton" class="singleButton numericButton">.</div>
      <div id="equalButton" class="singleButton">=</div>

    </div>
  </div>

  <script>

      
  const inputScreen = document.getElementById('input');
  
  const cButton = document.getElementById('cButton');
  const MoodButton = document.getElementById('negativePositiveButton');
  const percentageButton = document.getElementById('percentageButton');
  
  const divisionButton = document.getElementById('divisionButton');
  const xButton = document.getElementById('xButton');
  const minusButton = document.getElementById('minusButton');
  const plusButton = document.getElementById('plusButton');
  const equalButton = document.getElementById('equalButton');
  
  
  
  const numericButtonArr = [...document.getElementsByClassName('numericButton')];
  const operationButtonArr = [xButton, minusButton, plusButton];
  
  //button's events
  cButton.addEventListener('click', clearData);
  numericButtonArr.forEach(numericButton => {
    numericButton.addEventListener('click', displayNumbers);
  });
  operationButtonArr.forEach(operationButton => {
    operationButton.addEventListener('click', clearForSecondNumber);
  });
  divisionButton.addEventListener('click', handleDivision);
  MoodButton.addEventListener('click', changeMood);
  percentageButton.addEventListener('click', calculatePercentage);
  equalButton.addEventListener('click', performOperation);
  
  //variables
  let currentNumber = 0;
  let firstNumber = 0;
  let selectedOperation = '';
  let displayValue = '';
  
  let secondNumber = '';
  
  //Clear all data for C button press
  function clearData() {
    inputScreen.value = '';
    currentNumber = 0;
    displayValue = '';
    firstNumber = 0;
    selectedOperation = '';
  }
  
  //display numeric and dot buttons press on display
  function displayNumbers() {
    const value = this.textContent;
  
    if (value === '.') {
      // preventing dot duplication
      if (!displayValue.includes('.')) {
        displayValue += value;
        inputScreen.value = displayValue;
      }
    } else {
        displayValue += value;
        inputScreen.value = displayValue;
    }
  
    currentNumber = parseFloat(displayValue);
  }
  
  //convert - to + and + to-
  function changeMood(){
    currentNumber = currentNumber * (-1); 
    displayValue = currentNumber;
    inputScreen.value = displayValue;
  }
  
  //Calculate %
  function calculatePercentage(){
    currentNumber = currentNumber / (100);  
    displayValue = currentNumber;
    inputScreen.value = displayValue;
  }
  
  //for +,- and x. for division didnt work here :(
  function clearForSecondNumber() {
    selectedOperation = this.textContent;
    inputScreen.value = '';
    displayValue = '';
    firstNumber = currentNumber;
  }
  
  
  // Function to handle the division operation
  function handleDivision() {
    selectedOperation = '/';
    inputScreen.value = '';
    displayValue = '';
    firstNumber = currentNumber;
  }
  
  function performOperation() {
    secondNumber = currentNumber;
    let result;
  
    if (selectedOperation === 'x') {
      result = firstNumber * secondNumber;
    } else if (selectedOperation === '/') {
      result = firstNumber / secondNumber;
    } else if (selectedOperation === '-') {
      result = firstNumber - secondNumber;
    } else if (selectedOperation === '+') {
      result = firstNumber + secondNumber;
    }
  
    inputScreen.value = result;
    currentNumber = result;
    firstNumber = result;
    displayValue = '';


<details>
<summary>英文:</summary>

I&#39;m learning to code, this is just an exercise. any operation resulting in negative number will display wrong, but console.logs will display right.
why is that?

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>replit</title>
<style>

#calculator{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
max-width:400px;
min-width: 300px;
background-color: green;
}

#screen{
width: 100%;
aspect-ratio: 4/1.5;
background-color: rgba(0, 0, 0, 0.8);
}

#input{
width:90%;
height:90%;
background-color:rgba(0, 0, 0, 0);
color:white;
font-size: 15vw;
direction: rtl;
font-family: Helvetica, sans-serif;
}

#buttonsGrid{
width: 100%;
aspect-ratio: 4/5;
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(5, 1fr);
justify-content: center;
font-size: 5vw;
color:white;
background-color: gray;
font-family: Helvetica, sans-serif;
}

.singleButton{
border: solid;
border-color: black;
border-width: 1px;
display: flex;
align-items: center;
justify-content: center;

}

[id="0Button"] {
grid-column: span 2;
}

#equalButton, #plusButton, #minusButton, #xButton, #divisionButton{
background-color: orange;
}

#cButton, #negativePositiveButton, #percentageButton {
background-color: rgba(0, 0, 0, 0.3);
}

</style>

</head>

<body>

<h1>Simple calculator using JS</h1>

<div id="calculator">
<div id="screen">
<input inputmode="numeric" id="input">
</input>
</div>

&lt;div id=&quot;buttonsGrid&quot;&gt;
&lt;div id=&quot;cButton&quot; class=&quot;singleButton&quot;&gt;C
&lt;/div&gt;
&lt;div id=&quot;negativePositiveButton&quot; class=&quot;singleButton&quot;&gt;+/-
&lt;/div&gt;
&lt;div id=&quot;percentageButton&quot; class=&quot;singleButton&quot;&gt;%
&lt;/div&gt;
&lt;div id=&quot;divisionButton&quot; class=&quot;singleButton&quot;&gt;&#247;
&lt;/div&gt;
&lt;div id=&quot;7Button&quot; class=&quot;singleButton numericButton&quot;&gt;7&lt;/div&gt;
&lt;div id=&quot;8Button&quot; class=&quot;singleButton numericButton&quot;&gt;8&lt;/div&gt;
&lt;div id=&quot;9Button&quot; class=&quot;singleButton numericButton&quot;&gt;9&lt;/div&gt;
&lt;div id=&quot;xButton&quot; class=&quot;singleButton&quot;&gt;x&lt;/div&gt;
&lt;div id=&quot;4Button&quot; class=&quot;singleButton numericButton&quot;&gt;4&lt;/div&gt;
&lt;div id=&quot;5Button&quot; class=&quot;singleButton numericButton&quot;&gt;5&lt;/div&gt;
&lt;div id=&quot;6Button&quot; class=&quot;singleButton numericButton&quot;&gt;6&lt;/div&gt;
&lt;div id=&quot;minusButton&quot; class=&quot;singleButton&quot;&gt;-&lt;/div&gt;
&lt;div id=&quot;1Button&quot; class=&quot;singleButton numericButton&quot;&gt;1&lt;/div&gt;
&lt;div id=&quot;2Button&quot; class=&quot;singleButton numericButton&quot;&gt;2&lt;/div&gt;
&lt;div id=&quot;3Button&quot; class=&quot;singleButton numericButton&quot;&gt;3&lt;/div&gt;
&lt;div id=&quot;plusButton&quot; class=&quot;singleButton&quot;&gt;+&lt;/div&gt;
&lt;div id=&quot;0Button&quot; class=&quot;singleButton numericButton&quot;&gt;0&lt;/div&gt;
&lt;div id=&quot;dotButton&quot; class=&quot;singleButton numericButton&quot;&gt;.&lt;/div&gt;
&lt;div id=&quot;equalButton&quot; class=&quot;singleButton&quot;&gt;=&lt;/div&gt;
&lt;/div&gt;

</div>

<script >

const inputScreen = document.getElementById('input');

const cButton = document.getElementById('cButton');
const MoodButton = document.getElementById('negativePositiveButton');
const percentageButton = document.getElementById('percentageButton');

const divisionButton = document.getElementById('divisionButton');
const xButton = document.getElementById('xButton');
const minusButton = document.getElementById('minusButton');
const plusButton = document.getElementById('plusButton');
const equalButton = document.getElementById('equalButton');

const numericButtonArr = [...document.getElementsByClassName('numericButton')];
const operationButtonArr = [xButton, minusButton, plusButton];

//button's events
cButton.addEventListener('click', clearData);
numericButtonArr.forEach(numericButton => {
numericButton.addEventListener('click', displayNumbers);
});
operationButtonArr.forEach(operationButton => {
operationButton.addEventListener('click', clearForSecondNumber);
});
divisionButton.addEventListener('click', handleDivision);
MoodButton.addEventListener('click', changeMood);
percentageButton.addEventListener('click', calculatePercentage);
equalButton.addEventListener('click', performOperation);

//variables
let currentNumber = 0;
let firstNumber = 0;
let selectedOperation = '';
let displayValue = '';

let secondNumber = '';

//Clear all data for C button press
function clearData() {
inputScreen.value = '';
currentNumber = 0;
displayValue = '';
firstNumber = 0;
selectedOperation = '';
}

//display numeric and dot buttons press on display
function displayNumbers() {
const value = this.textContent;

if (value === &#39;.&#39;) {
// preventing dot duplication
if (!displayValue.includes(&#39;.&#39;)) {
displayValue += value;
inputScreen.value = displayValue;
}
} else {
displayValue += value;
inputScreen.value = displayValue;
}
currentNumber = parseFloat(displayValue);

}

//convert - to + and + to-
function changeMood(){
currentNumber = currentNumber * (-1);
displayValue = currentNumber;
inputScreen.value = displayValue;
}

//Calculate %
function calculatePercentage(){
currentNumber = currentNumber / (100);
displayValue = currentNumber;
inputScreen.value = displayValue;
}

//for +,- and x. for division didnt work here 为什么负数在这个简单的HTML和JavaScript计算器中显示为”5-“而不是”-5″?
function clearForSecondNumber() {
selectedOperation = this.textContent;
inputScreen.value = '';
displayValue = '';
firstNumber = currentNumber;
}

// Function to handle the division operation
function handleDivision() {
selectedOperation = '/';
inputScreen.value = '';
displayValue = '';
firstNumber = currentNumber;
}

function performOperation() {
secondNumber = currentNumber;
let result;

if (selectedOperation === &#39;x&#39;) {
result = firstNumber * secondNumber;
} else if (selectedOperation === &#39;/&#39;) {
result = firstNumber / secondNumber;
} else if (selectedOperation === &#39;-&#39;) {
result = firstNumber - secondNumber;
} else if (selectedOperation === &#39;+&#39;) {
result = firstNumber + secondNumber;
}
inputScreen.value = result;
currentNumber = result;
firstNumber = result;
displayValue = &#39;&#39;;

}

</script>

</body>

</html>


</details>
# 答案1
**得分**: 8
因为您通过设置 `direction: rtl;` 将书写方向设置为从右到左(RTL),这对像阿拉伯语这样的语言是有道理的。
也许您只是想将文本右对齐(而不是改变其方向),在这种情况下,您需要使用 `text-align: right;`。
<details>
<summary>英文:</summary>
Because you set the writing direction to RTL (right-to-left) by setting `direction: rtl;`. This makes sense for languages like Arabic. 
Perhaps you wanted to simply right-align the text (without reversing its direction), in this case you need `text-align: right;`. 
</details>

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

发表评论

匿名网友

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

确定