我有一个学校项目要求我使用if / else语句构建基本的Javascript计算器.我对这门语言(以及一般而言的编程语言)刚起步,在应用所学语法以及如何成功解决问题时遇到了麻烦.
接下来是代码:正如您将在HTML文件的注释中看到的那样,所有javascript代码都必须在js文件中.首先,我需要绑定onclick事件,方法是添加代码以将所有输入元素都放入一个称为输入的Array中(我已经完成),然后使用for循环遍历一个数组(我已经开始).然后,在for循环中,我需要使用if / else语句跳过不是按钮的输入元素,并将其他输入元素的onclick处理程序设置为在其内部调用calcu变量的函数.这是我如何完成此任务的地方.
我还需要确保将input [i] .id也传递给Calcu.然后,当然,我必须使用if / else语句完成calcu()函数.
在js文件中,我插入了注释以显示我的思考过程和我要求自己解决该问题的问题.
您能给我任何帮助或指示,我将不胜感激.谢谢!
var calcu = function(calcValue) {
/* "calc.output.value += "1";"" use to output numbers? */
if (calcValue) {
}
};
document.getElementById(inputs[i].id).onclick = calcu;
for (var i = 0; i <= inputs[id].length, i++) {
/* input button elements onlick handler needs to be set to (equal to?) a
function that calls the calcu variable inside of it */
if (input.type = button) {
console.log()
/* Need input that is not a button to be skipped*/
} else(input.type != button) {
}
}
<body>
<div id="content">
<div id="calculator-container">
<!-- DO NOT CHANGE THE FORM! Only use external JavaScript files -->
<form name="calc">
<label for="output">A Basic Calculator</label>
<!-- the name of the textbox is "output" -->
<input id="output" name="output" type="text" readonly>
<br>
<input type="button" id="1" value=" 1 ">
<input type="button" id="2" value=" 2 ">
<input type="button" id="3" value=" 3 ">
<input type="button" id="+" value=" + ">
<br>
<input type="button" id="4" value=" 4 ">
<input type="button" id="5" value=" 5 ">
<input type="button" id="6" value=" 6 ">
<input type="button" id="-" value=" - ">
<br>
<input type="button" id="7" value=" 7 ">
<input type="button" id="8" value=" 8 ">
<input type="button" id="9" value=" 9 ">
<input type="button" id="*" value=" x ">
<br>
<input type="button" id="c" value=" C ">
<input type="button" id="0" value=" 0 ">
<input type="button" id="equate" value=" = ">
<input type="button" id="/" value="÷">
</form>
</div>
</div>
</body>
解决方法:
我发现了一些基本的错误,例如;或者=如果您想将条件放在其他部分中,也必须丢失,否则必须使用else if条件.您需要立即在for循环中传递输入值.
var calcu = function(calcValue) {
if (calcValue) {
}
}
document.getElementById(inputs[i].id).onclick = calcu;
for (var i = 0; i <= inputs[id].length; i++)
{
if (input.type == button)
{
console.log();
}
else if(input.type != button) {
}
}
这是您的html代码.
<body>
<div id="content">
<div id="calculator-container">
<!-- DO NOT CHANGE THE FORM! Only use external JavaScript files -->
<form name="calc">
<label for="output">A Basic Calculator</label>
<!-- the name of the textbox is "output" -->
<input id="output" name="output" type="text" readonly>
<br>
<input type="button" id="1" value=" 1 ">
<input type="button" id="2" value=" 2 ">
<input type="button" id="3" value=" 3 ">
<input type="button" id="+" value=" + ">
<br>
<input type="button" id="4" value=" 4 ">
<input type="button" id="5" value=" 5 ">
<input type="button" id="6" value=" 6 ">
<input type="button" id="-" value=" - ">
<br>
<input type="button" id="7" value=" 7 ">
<input type="button" id="8" value=" 8 ">
<input type="button" id="9" value=" 9 ">
<input type="button" id="*" value=" x ">
<br>
<input type="button" id="c" value=" C ">
<input type="button" id="0" value=" 0 ">
<input type="button" id="equate" value=" = ">
<input type="button" id="/" value="÷">
</form>
</div>
</div>
</body>
希望这会帮助你.