[英]Creating a clear button code for a javascript calculator
I have an assignment where I have to create a calculator using java script and HTML.我有一个作业,我必须使用 java 脚本和 HTML 创建一个计算器。 I have coded enough that when I use the operators it will do the math, but the numbers will not show in the display box.我已经编写了足够多的代码,当我使用运算符时,它会进行数学运算,但数字不会显示在显示框中。 I also am unsure on how to code the clear button and the negate button.我也不确定如何对清除按钮和否定按钮进行编码。 Here is the external javascript file.这是外部 javascript 文件。
var mycalc = new calc(null,null,null);
function calc(firstNum,secNum,operator)
{
this.firstNum = firstNum;
this.secNum = secNum;
this.operator = operator;
}
function numberClick(ref)
{
//alert.ref.value
if(mycalc.firstNum == null)
mycalc.firstNum = ref.value
else if (mycalc.secNum == null)
mycalc.secNum = ref.value //ref.value is the value from the input
//alert("firstNum" + mycalc.operator.firstNum);
//alert("secNum") + mycalc.secNum);
}
function operatorClick(ref)
{
if (mycalc.operator == null)
mycalc.operator = ref.value;
}
function calculateResultClick()
{
if (mycalc.operator == "+")
{
var ans = parseInt(mycalc.firstNum) + parseInt(mycalc.secNum);
alert(ans);
}
if (mycalc.operator == "-")
{
var ans2 = parseInt(mycalc.firstNum) - parseInt(mycalc.secNum);
alert(ans2);
}
if (mycalc.operator == "*")
{
var ans3 = parseInt(mycalc.firstNum) * parseInt(mycalc.secNum);
alert(ans3)
}
if (mycalc.operator == "/")
{
var ans4 = parseInt(mycalc.firstNum) / parseInt(mycalc.secNum);
alert(ans4)
}
}
function Clear()
{
}
Negate:否定:
if(parseInt(mycalc.firstNum)>0){
answer=(mycalc.firstNum-mycalc.firstNum*2)
}
if(parseInt(mycalc.firstNum)<0){
answer=(mycalc.firstNum+mycalc.firstNum*2)
}
Clear清除
mycalc.firstNum=0;
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function Add() {
var x, y, txtResult;
x = document.getElementById('txtFirst').value;
y = document.getElementById('txtSecond').value;
if (x == " " || y == "") {
alert("Please enter FirstValue and SecondValue");
}
else{
var txtResult = +x + +y;
document.getElementById('txtResult').innerHTML = "Result: " + txtResult;
}
}
function Sub() {
var x, y, txtResult;
x = document.getElementById('txtFirst').value;
y = document.getElementById('txtSecond').value;
if (x == " " || y == "") {
alert("Please enter FirstValue and SecondValue");
} else {
var txtResult = +x - +y;
document.getElementById('txtResult').innerHTML = "Result: " + txtResult;
}
}
function Mul() {
var x, y, txtResult;
x = document.getElementById('txtFirst').value;
y = document.getElementById('txtSecond').value;
if (x == " " || y == "") {
alert("Please enter FirstValue and SecondValue");
} else {
var txtResult = +x * +y;
document.getElementById('txtResult').innerHTML = "Result: " + txtResult;
}
}
function Div() {
var x, y, txtResult;
x = document.getElementById('txtFirst').value;
y = document.getElementById('txtSecond').value;
if (x == " " || y == "") {
alert("Please enter FirstValue and SecondValue");
}
else if (y != 0) {
var txtResult = +x / +y;
}
else {
alert("Second Number Should not be Zero");
}
document.getElementById('txtResult').innerHTML = "Result: " + txtResult;
}
function Clear() {
document.getElementById('txtFirst').value = "";
document.getElementById('txtSecond').value = "";
document.getElementById('txtResult').value = "";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<label> FirstNumber :</label><br />
<input id="txtFirst" type="text" /><br />
<label> Second Number :</label><br />
<input id="txtSecond" type="text" /><br />
<label id="txtResult"></label><br />
<input id="btnAdd" type="button" value="ADD" onclick="Add()"/>
<input id="btnSub" type="button" value="SUB" onclick="Sub()"/><br />
<input id="btnMul" type="button" value="MUL" onclick="Mul()"/>
<input id="btnDiv" type="button" value="DIV" onclick="Div()"/>
<input id="btnClear" type="button" value="Clear" onclick="Clear()"/>
</div>
</form>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.