JavaScript 我的测验中的计算器
JavaScript Calculator in my Quiz
这是我将用于测验的代码。
代码有什么问题?
On mouse over(button), the html will alert the description of the function, and clicking the button will run the specified function. Then, it alerts the user for the answer of the operation.
<script type="text/javascript">
function a(){
alert('Adds the two numbers.');}
function b(){
alert('Subtracts the second number from the first number.');}
function c(){
alert('Multiplies the two numbers.');}
function d(){
alert('Divides the second number from the first number.');}
function add(){
var ans = form.none.value + form.notwo.value;
alert(form.none.value + '+' +form.notwo.value + '=' + ans);
}
function subtract(){
var ans = form.notwo.value - form.none.value;
alert(form.notwo.value + '-' form.none.value+ '=' + ans);
}
function times(){
var ans = form.none.value * form.notwo.value;
alert(form.none.value + '*' +form.notwo.value + '=' + ans);
}
function over(){
var ans = form.notwo.value / form.none.value;
alert(form.notwo.value + '/' form.none.value+ '=' + ans);
}
</script>
<style>
h1{font-color: red;}
body{ background-color: aquamarine;}
</style>
<!DOCTYPE html>
<html>
<head><title> JAVAcalc </title></head>
<body>
<h1> The Java Calculator </h1>
<form name="me">
Enter all of the textfields required and click the button to use an operation.<br>
First number: <input type = "textbox" name="none" id="none"value="0">
Second number: <input type = "textbox" name="notwo" id="none" value="0"><br>
<input type = "button" name="none" value="Add!" onclick="add()" onmouseover="a()">
<input type = "button" name="none" value="Subtract!" onclick="subtract()" onmouseover="b()">
<input type = "button" name="none" value="Multiply!" onclick="times()" onmouseover="c()">
<input type = "button" name="none" value="Divide!" onclick="over()" onmouseover="d()">
</body>
</html>
提前致谢!祝你有美好的一天!
由于其中的错误,您的代码 运行 没有。
alert(form.notwo.value + '/' form.none.value+ '=' + ans);
例如,漏掉了一个“+”,应该是这样的:
alert(form.notwo.value + '/' + form.none.value+ '=' + ans);
类似的还有很多,但我相信您会自己找到的。
接下来尝试为您的两个数字输入字段指定一个 ID 并获取如下值:
var ans = document.getElementById('ID of your input').value + document.getElementById('ID of your input'.value;
应该删除鼠标悬停的效果,这很烦人;-)
要检查未来的 JS 错误,请使用浏览器开发人员工具中的控制台(通常是 F12)
代码:
<script type="text/javascript">
function a(){
alert('Adds the two numbers.');}
function b(){
alert('Subtracts the second number from the first number.');}
function c(){
alert('Multiplies the two numbers.');}
function d(){
alert('Divides the second number from the first number.');}
function e(){
alert('First number = opposite; Second number = hypotenuse; Finds the sine ratio.');}
function f(){
alert('First number = adjacent; Second number = hypotenuse; Finds the cosine ratio.');}
function g(){
alert('First number = opposite; Second number = adjacent; Finds the tangent ratio.');}
function h(){
alert('First number = hypotenuse; Second number = adjacent; Finds the cosecant ratio.');}
function i(){
alert('First number = hypotenuse; Second number = opposite; Finds the secant ratio.');}
function j(){
alert('First number = adjacent; Second number = opposite; Finds the cotangent ratio.');}
function plus(){
var ans = (me.y.value*1) + (me.x.value*1);
alert(me.x.value + '+' + me.y.value + '=' + ans);
}
function subtract(){
var ans = me.y.value - me.x.value;
alert(me.y.value + '-' + me.x.value+ '=' + ans);
}
function times(){
var ans = me.x.value * me.y.value;
alert(me.x.value + '*' + me.y.value + '=' + ans);
}
function over(){
var ans = me.y.value / me.x.value;
alert(me.y.value + '/' + me.x.value+ '=' + ans);
}
function sine(){
var ans = Math.sin(me.x.value , me.y.value);
alert('The answer is ' + ans + '.');
}
function cosine(){
var ans = Math.cos(me.x.value , me.y.value);
alert('The answer is ' + ans + '.');
}
function tangent(){
var ans = Math.tan(me.x.value , me.y.value);
alert('The answer is ' + ans + '.');
}
function cosecant(){
var ans = 1/Math.sin(me.x.value , me.y.value);
alert('The answer is ' + ans + '.');
}
function secant(){
var ans = 1/Math.cos(me.x.value , me.y.value);
alert('The answer is ' + ans + '.');
}
function cotangent(){
var ans = 1/Math.tan(me.x.value , me.y.value);
alert('The answer is ' + ans + '.');
}
</script>
<style>
h1{font-color: red;}
body{ background-color: aquamarine;}
section{ -webkit-column-count: 2;
-moz-column-count: 2;
column-count: 2;
-webkit-column-gap: 20px;
-moz-column-gap: 20px;
column-gap: 20px;}
</style>
<!DOCTYPE html>
<html>
<head><title> JAVAcalc </title></head>
<body>
<h1> The Java Calculator </h1>
<form name="me">
Enter all of the textfields required and click the button to use an operation.<br>
First number: <input type = "textbox" name="x" id="none"value="0">
Second number: <input type = "textbox" name="y" id="none" value="0"><br><br><br>
<section>
<input type = "button" name="none" value="Add!" onclick="plus()" onmouseout="a()"><br><br>
<input type = "button" name="none" value="Subtract!" onclick="subtract()" onmouseout="b()"><br><br>
<input type = "button" name="none" value="Multiply!" onclick="times()" onmouseout="c()"><br><br>
<input type = "button" name="none" value="Divide!" onclick="over()" onmouseout="d()"><br><br>
<input type = "button" name="none" value="Sine!" onclick="sine()" onmouseout="e()"><br><br>
<input type = "button" name="none" value="Cosine" onclick="cosine()" onmouseout="f()"><br><br>
<input type = "button" name="none" value="Tangent!" onclick="tangent()" onmouseout="g()"><br><br>
<input type = "button" name="none" value="Secant!" onclick="cosecant()" onmouseout="h()"><br><br>
<input type = "button" name="none" value="Cosecant!" onclick="secant()" onmouseout="i()"><br><br>
<input type = "button" name="none" value="Cotangent!" onclick="cotangent()" onmouseout="j()"><br><br>
</section>
</form>
</body>
</html>
这是我将用于测验的代码。 代码有什么问题?
On mouse over(button), the html will alert the description of the function, and clicking the button will run the specified function. Then, it alerts the user for the answer of the operation.
<script type="text/javascript">
function a(){
alert('Adds the two numbers.');}
function b(){
alert('Subtracts the second number from the first number.');}
function c(){
alert('Multiplies the two numbers.');}
function d(){
alert('Divides the second number from the first number.');}
function add(){
var ans = form.none.value + form.notwo.value;
alert(form.none.value + '+' +form.notwo.value + '=' + ans);
}
function subtract(){
var ans = form.notwo.value - form.none.value;
alert(form.notwo.value + '-' form.none.value+ '=' + ans);
}
function times(){
var ans = form.none.value * form.notwo.value;
alert(form.none.value + '*' +form.notwo.value + '=' + ans);
}
function over(){
var ans = form.notwo.value / form.none.value;
alert(form.notwo.value + '/' form.none.value+ '=' + ans);
}
</script>
<style>
h1{font-color: red;}
body{ background-color: aquamarine;}
</style>
<!DOCTYPE html>
<html>
<head><title> JAVAcalc </title></head>
<body>
<h1> The Java Calculator </h1>
<form name="me">
Enter all of the textfields required and click the button to use an operation.<br>
First number: <input type = "textbox" name="none" id="none"value="0">
Second number: <input type = "textbox" name="notwo" id="none" value="0"><br>
<input type = "button" name="none" value="Add!" onclick="add()" onmouseover="a()">
<input type = "button" name="none" value="Subtract!" onclick="subtract()" onmouseover="b()">
<input type = "button" name="none" value="Multiply!" onclick="times()" onmouseover="c()">
<input type = "button" name="none" value="Divide!" onclick="over()" onmouseover="d()">
</body>
</html>
提前致谢!祝你有美好的一天!
由于其中的错误,您的代码 运行 没有。
alert(form.notwo.value + '/' form.none.value+ '=' + ans);
例如,漏掉了一个“+”,应该是这样的:
alert(form.notwo.value + '/' + form.none.value+ '=' + ans);
类似的还有很多,但我相信您会自己找到的。
接下来尝试为您的两个数字输入字段指定一个 ID 并获取如下值:
var ans = document.getElementById('ID of your input').value + document.getElementById('ID of your input'.value;
应该删除鼠标悬停的效果,这很烦人;-) 要检查未来的 JS 错误,请使用浏览器开发人员工具中的控制台(通常是 F12)
代码:
<script type="text/javascript">
function a(){
alert('Adds the two numbers.');}
function b(){
alert('Subtracts the second number from the first number.');}
function c(){
alert('Multiplies the two numbers.');}
function d(){
alert('Divides the second number from the first number.');}
function e(){
alert('First number = opposite; Second number = hypotenuse; Finds the sine ratio.');}
function f(){
alert('First number = adjacent; Second number = hypotenuse; Finds the cosine ratio.');}
function g(){
alert('First number = opposite; Second number = adjacent; Finds the tangent ratio.');}
function h(){
alert('First number = hypotenuse; Second number = adjacent; Finds the cosecant ratio.');}
function i(){
alert('First number = hypotenuse; Second number = opposite; Finds the secant ratio.');}
function j(){
alert('First number = adjacent; Second number = opposite; Finds the cotangent ratio.');}
function plus(){
var ans = (me.y.value*1) + (me.x.value*1);
alert(me.x.value + '+' + me.y.value + '=' + ans);
}
function subtract(){
var ans = me.y.value - me.x.value;
alert(me.y.value + '-' + me.x.value+ '=' + ans);
}
function times(){
var ans = me.x.value * me.y.value;
alert(me.x.value + '*' + me.y.value + '=' + ans);
}
function over(){
var ans = me.y.value / me.x.value;
alert(me.y.value + '/' + me.x.value+ '=' + ans);
}
function sine(){
var ans = Math.sin(me.x.value , me.y.value);
alert('The answer is ' + ans + '.');
}
function cosine(){
var ans = Math.cos(me.x.value , me.y.value);
alert('The answer is ' + ans + '.');
}
function tangent(){
var ans = Math.tan(me.x.value , me.y.value);
alert('The answer is ' + ans + '.');
}
function cosecant(){
var ans = 1/Math.sin(me.x.value , me.y.value);
alert('The answer is ' + ans + '.');
}
function secant(){
var ans = 1/Math.cos(me.x.value , me.y.value);
alert('The answer is ' + ans + '.');
}
function cotangent(){
var ans = 1/Math.tan(me.x.value , me.y.value);
alert('The answer is ' + ans + '.');
}
</script>
<style>
h1{font-color: red;}
body{ background-color: aquamarine;}
section{ -webkit-column-count: 2;
-moz-column-count: 2;
column-count: 2;
-webkit-column-gap: 20px;
-moz-column-gap: 20px;
column-gap: 20px;}
</style>
<!DOCTYPE html>
<html>
<head><title> JAVAcalc </title></head>
<body>
<h1> The Java Calculator </h1>
<form name="me">
Enter all of the textfields required and click the button to use an operation.<br>
First number: <input type = "textbox" name="x" id="none"value="0">
Second number: <input type = "textbox" name="y" id="none" value="0"><br><br><br>
<section>
<input type = "button" name="none" value="Add!" onclick="plus()" onmouseout="a()"><br><br>
<input type = "button" name="none" value="Subtract!" onclick="subtract()" onmouseout="b()"><br><br>
<input type = "button" name="none" value="Multiply!" onclick="times()" onmouseout="c()"><br><br>
<input type = "button" name="none" value="Divide!" onclick="over()" onmouseout="d()"><br><br>
<input type = "button" name="none" value="Sine!" onclick="sine()" onmouseout="e()"><br><br>
<input type = "button" name="none" value="Cosine" onclick="cosine()" onmouseout="f()"><br><br>
<input type = "button" name="none" value="Tangent!" onclick="tangent()" onmouseout="g()"><br><br>
<input type = "button" name="none" value="Secant!" onclick="cosecant()" onmouseout="h()"><br><br>
<input type = "button" name="none" value="Cosecant!" onclick="secant()" onmouseout="i()"><br><br>
<input type = "button" name="none" value="Cotangent!" onclick="cotangent()" onmouseout="j()"><br><br>
</section>
</form>
</body>
</html>