0

如何在我的 JavaScript/HTML 计算器中添加平方根?已经尝试添加它至少 3 小时 :(

另外,我如何制作一个将数字与自身相乘的按钮(也在我的计算器中)?

<!DOCTYPE html>
<html>
<head>
<title>Calc</title>
<script>
function myFunction(clickedId) {
    document.calc.result.value+=clickedId;
}
function Clear() {
    document.calc.result.value="";
}
function compute() {
    try{
    var inp=eval(document.calc.result.value);
    document.calc.result.value=inp;
    }catch(err){
            document.calc.result.value="error";
    }
}
function doMath(){
var inputNum1=document.form1.input1.value;
var result = Math.sqrt(inputNum1);
document.form1.answer.value = result;
}
</script>
</head>
<body>
<center>
    <form name="calc">
    <input type="text" name="result" size="30px" readonly>
    </form>
<table>
  <tr>
    <td><button type="button" id="1" onclick="myFunction(this.id)">1</button></td> <!--Skaitlis 1-->
    <td><button type="button" id="2" onclick="myFunction(this.id)">2</button></td> <!--Skaitlis 2-->
    <td><button type="button" id="3" onclick="myFunction(this.id)">3</button></td> <!--Skaitlis 3-->
  </tr>
  <tr>
    <td><button type="button" id="4" onclick="myFunction(this.id)">4</button></td> <!--Skaitlis 4-->
    <td><button type="button" id="5" onclick="myFunction(this.id)">5</button></td> <!--Skaitlis 5-->
    <td><button type="button" id="6" onclick="myFunction(this.id)">6</button></td> <!--Skaitlis 6-->
  </tr>
  <tr>
    <td><button type="button" id="7" onclick="myFunction(this.id)">7</button></td> <!--Skaitlis 7-->
    <td><button type="button" id="8" onclick="myFunction(this.id)">8</button></td> <!--Skaitlis 8-->
    <td><button type="button" id="9" onclick="myFunction(this.id)">9</button></td> <!--Skaitlis 9-->
  </tr>
  <tr>
    <td><button type="button" id="0" onclick="myFunction(this.id)">0</button></td> <!--Skaitlis 0-->
    <td><button type="button" id="+" onclick="myFunction(this.id)">+</button></td> <!--Plusa zīme-->
    <td><button type="button" id="-" onclick="myFunction(this.id)">-</button></td> <!--Mīnusa zīme-->
  </tr>
  <tr>
    <td><button type="button" id="*" onclick="myFunction(this.id)">x</button></td> <!--Reizināšanas zīme-->
    <td><button type="button" id="/" onclick="myFunction(this.id)">÷</button></td> <!--Dalīšanas zīme-->
    <td><button type="button" id="ANS" onclick="compute()">=</button></td> <!--Vienādības zīme-->
  </tr>
</table>

<td><button type="button" id="CLEAR" onclick="Clear()">c</button></td> <!--Izdzēst rezultātu-->
<td><button type="button" id="3.141592653589793" onclick="myFunction(this.id)">π</button></td> <!--Skaitlis 3.14...-->
<td><button type="button" id="6.283185307179586" onclick="myFunction(this.id)">τ</button></td> <!--Skaitlis 6.28...-->
</center>
</body>
</html>

4

1 回答 1

1

function myFunction(clickedId) {
    document.calc.result.value+=clickedId;
}
function Clear() {
    document.calc.result.value="";
}
function compute() {
    try{
    var inp=eval(document.calc.result.value);
    document.calc.result.value=inp;
    }catch(err){
            document.calc.result.value="error";
    }
}
function doMath(){
  var inputNum1=document.calc.result.value;
  var result = Math.sqrt(inputNum1);
  document.calc.result.value = result;
}
<!DOCTYPE html>
<html>
<head>
<title>Calc</title>
<script>

</script>
</head>
<body>
<center>
    <form name="calc">
    <input type="text" name="result" size="30px" readonly>
    </form>
<table>
  <tr>
    <td><button type="button" id="1" onclick="myFunction(this.id)">1</button></td> <!--Skaitlis 1-->
    <td><button type="button" id="2" onclick="myFunction(this.id)">2</button></td> <!--Skaitlis 2-->
    <td><button type="button" id="3" onclick="myFunction(this.id)">3</button></td> <!--Skaitlis 3-->
  </tr>
  <tr>
    <td><button type="button" id="4" onclick="myFunction(this.id)">4</button></td> <!--Skaitlis 4-->
    <td><button type="button" id="5" onclick="myFunction(this.id)">5</button></td> <!--Skaitlis 5-->
    <td><button type="button" id="6" onclick="myFunction(this.id)">6</button></td> <!--Skaitlis 6-->
  </tr>
  <tr>
    <td><button type="button" id="7" onclick="myFunction(this.id)">7</button></td> <!--Skaitlis 7-->
    <td><button type="button" id="8" onclick="myFunction(this.id)">8</button></td> <!--Skaitlis 8-->
    <td><button type="button" id="9" onclick="myFunction(this.id)">9</button></td> <!--Skaitlis 9-->
  </tr>
  <tr>
    <td><button type="button" id="0" onclick="myFunction(this.id)">0</button></td> <!--Skaitlis 0-->
    <td><button type="button" id="+" onclick="myFunction(this.id)">+</button></td> <!--Plusa zīme-->
    <td><button type="button" id="-" onclick="myFunction(this.id)">-</button></td> <!--Mīnusa zīme-->
  </tr>
  <tr>
    <td><button type="button" id="*" onclick="myFunction(this.id)">x</button></td> <!--Reizināšanas zīme-->
    <td><button type="button" id="/" onclick="myFunction(this.id)">÷</button></td> <!--Dalīšanas zīme-->
    <td><button type="button" id="ANS" onclick="compute()">=</button></td> <!--Vienādības zīme-->
  </tr>
  <tr>
    <td><button type="button" id="CLEAR" onclick="Clear()">c</button></td><!--Izdzēst rezultātu-->
    <td><button type="button" id="3.141592653589793" onclick="myFunction(this.id)">π</button></td> <!--Skaitlis 3.14...-->
    <td><button type="button" id="6.283185307179586" onclick="myFunction(this.id)">τ</button></td> <!--Skaitlis 6.28...-->
  </tr>
  <tr>
    <td><button type="button" id="SQRT" onclick="doMath()">√&lt;/button></td><!--Izdzēst rezultātu-->
    <td><button type="button" > </button></td> <!--Skaitlis 3.14...-->
    <td><button type="button" > </button></td> <!--Skaitlis 6.28...-->
  </tr>
</table>


</center>
</body>
</html>

添加到片段中。请检查您的功能是否正确您只需要引用变量

于 2015-08-20T12:11:41.740 回答