0
\$\begingroup\$

I’m trying to make a simple calculator with 3 inputs in JavaScript but it doesn’t work. For now my code is:

<html>
<input placeholder="Number" id="Number F” class="Number F" onchange="calculator()">
<input placeholder="/ ,  * ,  -  or +" id="Number O" class="Number O" onchange="calculator()">
<input placeholder="Number" id="Number S" class="Number S" onchange="calculator()">
<p id="resul"></p>
<button id="button" onclick="calculator()"></button>
<script>
function calculator() {
  var cel = document.getElementById("Number F").value;
  var celular = document.getElementById("Number S").value;
  var operator = document.getElementById("Number 0").value;
  var result = document.getElementById("resul").innerHTML;
  var getAverage = (numberOne, numberTwo) => {
    const average = numberOne / numberTwo;
    result = average;
    if (operator === "+" || operator === "/" || operator === "-" || operator === "*") {
       getAverage(cel, celular);
     } else {
};

</script>
</html>

I’m beginner in coding. What is wrong? Please Help Me! Thank you!

\$\endgroup\$

closed as off-topic by Ludisposed, t3chb0t, Toby Speight, pacmaninbw, Mast Dec 8 '17 at 15:45

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Ludisposed, t3chb0t, Toby Speight, pacmaninbw, Mast
If this question can be reworded to fit the rules in the help center, please edit the question.

2
\$\begingroup\$

Id attribute cannot contains spaces.

You can also use an object which contains the function's definition for all the possible operations.

var operators = {
  '+': function(a, b) { return +a + (+b) },
  '-': function(a, b) { return a - b },
  '/': function(a, b) { return a / b },
  '*': function(a, b) { return a * b },
};

var operators = {
    '+': function(a, b) { return +a + (+b) },
    '-': function(a, b) { return a - b },
    '/': function(a, b) { return a / b },
    '*': function(a, b) { return a * b },
};
function calculator() {
  var cel = document.getElementById("NumberF").value;
 var celular = document.getElementById("NumberS").value;
  var operator = document.getElementById("NumberO").value;
  var result = document.getElementById("resul");
  if(cel && celular && operator)
    result.innerHTML = operators[operator](cel, celular);
};
<input placeholder="Number" id="NumberF" class="NumberF" onchange="calculator()">
<input placeholder="/ ,  * ,  -  or +" id="NumberO" class="NumberO" onchange="calculator()">
<input placeholder="Number" id="NumberS" class="NumberS" onchange="calculator()">
<p id="resul"></p>
<button id="button" onclick="calculator()">Click</button>

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.