javascript - 如何将平方根添加到我的 JavaScript/HTML 计算器中?

标签 javascript html calculator calc

如何将平方根添加到我的 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>

最佳答案

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()">√</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>

已添加到代码片段。请检查您的函数是否正确,您只需要引用变量

关于javascript - 如何将平方根添加到我的 JavaScript/HTML 计算器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32117784/

相关文章:

javascript - 舍入 n * 10 的算法

javascript - 不要重复已经重复的项目

javascript - ng-if 内部的插值

尽管提到了 Lib,jQuery 脚本仍无法正常工作

calculator - TI-83加: Store commonly used functions for easy access?

javascript - 为什么当应用程序关闭时 componentWillUnmount 不会在 React 根组件中触发

javascript - 为什么我的 for 循环改变了 jQuery 中 toggleClass 的行为

html - 全日历上下文菜单

javascript - 如何使用 Javascript 从输入框值中获取总和?

python - 在 python 中创建类似于 MS 计算器的 GUI 计算器