0

I'm trying to code a simple calculator function that takes two numbers and an operator. The example given to me by the guide I'm following is:

if (stringOperator === '+') {
  return num1 + num2;
}
else if (stringOperator === '-') {
  return num1 - num2;
}

However I wanted to try something different and store the operator (not a string) in a variable and calculate the result that way.

function miniCalculator(num1, num2, stringOperator) {
  let operator;
  if (stringOperator === '+') {
    operator = +
  }
  else if (stringOperator === '-') {
    operator = -
  }
  else if (stringOperator === '*') {
    operator = *
  }
  else if (stringOperator === '/') {
    operator = /
  }
  return Number(num1) operator Number(num2)
}

For the call miniCalculator(1, 2, "+") the return value would transform from Number(num1) operator Number(num2) into the actual calculation 1 + 2, thus returning 3.

Why does this not work? And how can I make it work?

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
Hojin Jang
  • 45
  • 4
  • 2
    What's your problem with the given code, besides the obvious syntax errors? What have you tried to resolve your problem? – Nico Haase Aug 25 '21 at 10:55
  • 4
    You can't assign an operator to a variable, though this question covers something similar: [Are Variable Operators Possible?](https://stackoverflow.com/questions/5834318/are-variable-operators-possible) – DBS Aug 25 '21 at 10:56
  • `= + ` and similar is syntax error. It should be `output1++`/`output1--`, while `= *` and `= /` must have other number to execute maths with – Justinas Aug 25 '21 at 10:57
  • `output2 = Number(num1) output1 Number(num2) ;` - that is not even clear what you try to do here. Concatenation? Some calculations? – Justinas Aug 25 '21 at 10:59
  • I guess you could just do `const miniCalculator = (num1, num2, operator) => eval( num1 + operator + num2 )` ? (Yes eval is evil, etc. but it works) – Jeremy Thille Aug 25 '21 at 11:23
  • @mjyazdani your edit was harmful. You completely changed OP's code and made it work directly in their question. That's not what you're supposed to do. I reverted back to OP's original question. – Jeremy Thille Aug 25 '21 at 11:27
  • @JeremyThille I wrote the correct complete code for him. But when I wanted to submit the answer, I determined that the question is closed! It's his first day in StackOverflow and he is a newbie! I just wanted him to have the answer in some way. – mjyazdani Aug 25 '21 at 11:33
  • 3
    I understand, and your intention was good, but editing OP's code directly in their question is considered harmful by Stackoverflow's rules. There's a reason why closing a question prohibits posting answers. Besides, people seeing OP's post for the first time will be completely lost, because the problem and the question won't match the code provided. I have posted a short one-liner solution in a comment above, so OP isn't left in the dark :) – Jeremy Thille Aug 25 '21 at 12:34

1 Answers1

0

I think your problem is handling operators. We can't assign an operator to a variable! below is a sample that works.

function miniCalculator(operator) {
    let output1 ;
    let num1 = Number(document.getElementById("num1").value);
    let num2 = Number(document.getElementById("num2").value);

    if (operator === '+') {
      output1 = num1 + num2;
    }
    else if (operator === '-') {
      output1 = num1 - num2;
    }
    else if (operator === '*') {
      output1 = num1 * num2;
    }
    else if (operator === '/') {
      output1 = num1 / num2;
    }
    document.getElementById('output1').value = output1;
}
<div>
    <label>Number1
        <input type="input" name="num1" id="num1">
    </label>
</div>
<div>
    <label>Number2
        <input type="input" name="num2" id="num2">
    </label>
</div>
<div>
    <button onclick="miniCalculator('+')" >+</button>
    <button onclick="miniCalculator('-')" >-</button>
    <button onclick="miniCalculator('*')" >*</button>
    <button onclick="miniCalculator('/')" >/</button>
</div>
<div>
    <label>Output
        <input type="input" name="output1" id="output1" disabled>
    </label>
</div>
mjyazdani
  • 2,110
  • 6
  • 33
  • 64