[英]I am new to Javascript and working on a calculator. How can I get num1, num2 and operators variables
for a simple calculator i have used a form butI cannot get it done I just need help with JS to find out the following: first I need to have the values of variables num1, num2 and operator and second I need to make a function that does the calculation once the user clicks the equal button. 对于一个简单的计算器我已经使用了一个表单但是我无法完成它我只需要JS的帮助就可以找到以下内容:首先我需要有变量num1,num2和operator的值,然后我需要创建一个函数来做用户单击相等按钮后的计算。 please help
请帮忙
<form name="myform">
<input type="text" name="display" id="input" value=""><br>
<input class="digit" type="button" value="7" id="7" value="7">
<input class="digit" type="button" id="8" value="8">
<input class="digit" type="button" id="9" value="9"><br>
<input class="digit" type="button" id="4" value="4">
<input class="digit" type="button" id="5" value="5">
<input class="digit" type="button" id="6" value="6">
<input class="digit" type="button" id="1" value="1"><br>
<input class="digit" type="button" id="2" value="2">
<input class="digit" type="button" id="3" value="3">
<input class="digit" type="button" id="0" value="0">
<input class="digit" type="button" id="." value="."><br>
<input class="operator" id="opr" type="button" value="+">
<input class="operator" id="opr" type="button" value="-">
<input class="operator" id="opr" type="button" value="*">
<input class="operator" id="opr" type="button" value="/">
<input class="operator" type="button" value="=">
</form>
<script>
let numbers =
document.getElementsByClassName('digit')
let outPut =
document.getElementById('input')
let operator =
document.getElementsByClassName('operator'
)
//this section of the code targets the numbers in
for (i = 0; i < numbers.length; i++)
numbers[i].addEventListener('click', a => {
var btn = a.target;
outPut.value += btn.value;
console.log(outPut.value);
})
//this section targets the operator keys
for (i = 0; i < operator.length; i++)
operator[i].addEventListener('click', a => {
var btn = a.target; if (operator.value != "") {
outPut.value = "";
num1 = parseInt(outPut.value);
})
</script>
A few things, other than that, nice job, I hope you're having fun! 一些事情,除此之外,干得好,我希望你玩得开心!
id="input"
id="input"
outPut.value = ""
outPut.value = ""
eval()
, but beware, using eval()
can open you up to injection attacks in a production app: Why is using the JavaScript eval function a bad idea? eval()
,但要注意,使用eval()
可以在生产应用程序中打开注入攻击: 为什么使用JavaScript eval函数是个坏主意? operator.value
but should be checking btn.value
in your last function operator.value
但应检查上一个函数中的btn.value
<form name="myform">
<input type="text" name="display" id="input" value=""><br>
<input class="digit" type="button" value="7">
<input class="digit" type="button" value="8">
<input class="digit" type="button" value="9"><br>
<input class="digit" type="button" value="4">
<input class="digit" type="button" value="5">
<input class="digit" type="button" value="6">
<input class="digit" type="button" value="1"><br>
<input class="digit" type="button" value="2">
<input class="digit" type="button" value="3">
<input class="digit" type="button" value="0">
<input class="digit" type="button" value="."><br>
<input class="operator" type="button" value="+">
<input class="operator" type="button" value="-">
<input class="operator" type="button" value="*">
<input class="operator" type="button" value="/">
<input class="operator" type="button" value="=">
</form>
<script>
let numbers = document.getElementsByClassName('digit')
let outPut = document.getElementById('input')
let operator = document.getElementsByClassName('operator')
outPut.value = ""
//this section of the code targets the numbers in
for (i = 0; i < numbers.length; i++)
numbers[i].addEventListener('click', a => {
var btn = a.target;
outPut.value += btn.value;
console.log(outPut.value);
})
//this section targets the operator keys
for (i = 0; i < operator.length; i++)
operator[i].addEventListener('click', a => {
var btn = a.target;
if (btn.value !== "=") {
outPut.value += btn.value;
num1 = parseInt(outPut.value);
} else {
outPut.value = eval(outPut.value)
}
})
</script>
Update : to avoid using eval()
you can do something like (note this doesn't handle decimals): 更新 :为了避免使用
eval()
你可以做类似的事情(注意这不处理小数):
outPut.value = ""
let num1 = 0
let op = ""
let num2 = 0
//this section of the code targets the numbers in
for (i = 0; i < numbers.length; i++)
numbers[i].addEventListener('click', a => {
var btn = a.target;
outPut.value += btn.value;
if (op === "") {
num1 = parseInt(btn.value);
} else {
num2 = parseInt(btn.value);
}
console.log(outPut.value);
})
//this section targets the operator keys
for (i = 0; i < operator.length; i++)
operator[i].addEventListener('click', a => {
var btn = a.target;
if (btn.value !== "=") {
outPut.value += btn.value;
op = btn.value;
} else {
if (op === "+") {
outPut.value = num1 + num2;
} else if (op === '-') {
outPut.value = num1 - num2;
} else if (op === '*') {
outPut.value = num1 * num2;
} else if (op === '/') {
outPut.value = num1 / num2;
}
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.