Issue with javascript calculator

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.

  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>
<button type="button" onclick="calc()">Calculate</button>

<div id="results"></div>


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…

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.