Hello everyone!
I was following a video tutorial on how to make a javascript calculator and I copied all the code but it does not seems to fully work for me.
I have two problems; one is that calc() function is not defined and the other being "private names are not valid in this context, refering to the variables used for function.
I may have overlooked something in the tutorial. All help is appreciated, thanks in advance.
<html>
<head>
</head>
<body>
<form>
Num1: <input type="text" id="num1">
Num2: <input type="text" id="num2">
Operator: <select id="operator">
<option value="add">Add</option>
<option value="sub">Subtract</option>
<option value="div">Divide</option>
<option value="mul">Multiply</option>
</select>
<button type="button" onclick="calc()">Calculate</button>
</form>
<div id="results"></div>
</body>
<script></script>
</html>
FOLLOWED BY
function calc() {
var a = parseInt(document.querySelector(#num1).value);
var b = parseInt(document.querySelector(#num2).value);
var op = document.querySelector(#operator).value;
var calculate;
if (op == "add") {
calculate = a + b;
} else if (op == "sub") {
calculate = a - b;
} else if (op == "div") {
calculate = a / b;
} else if (op == "mul") {
calculate = a * b;
}
document.querySelector(#results).innerHTML = calculate;
}
EDIT: Don’t know how to post raw HTML text without it executing in here…