0

I want to make it so you enter 2 numbers click a button and it performs the calculation, however it is not doing what I want it to and I am not sure as to why. Also the addition part is adding the numbers as though they are strings and I'm not sure why either.

<html>
<head>
    <script>
        function Calculation(Type) {
            let num1 = document.forms["Arithmetic"]["Number1"].value;
            let num2 = document.forms["Arithmetic"]["Number2"].value;
            if (Type = "ADD"){
                alert(num1+num2);
            }
            if (Type = "SUB"){
                alert(num1-num2);
            }
            if (Type = "MUL"){
                alert(num1*num2);
            }
            if (Type = "DIV"){
                alert(num1/num2);
            }
        }
    </script>
</head>
<body>
    <div style = "text-align: center">
        <form name="Arithmetic" onsubmit = "return Calculation()">
            Number1: <input id = "Number1" type = "number" name = "Number1">
            <br>
            Number2: <input id = "Number2" type = "number" name = "Number2">
            <br>
            <button onclick="Calculation("ADD")">Add</button>
            <button onclick="Calculation("SUB")">Subtract</button>
            <button onclick="Calculation("MUL")">Multiply</button>
            <button onclick="Calculation("DIV")">Divide</button>
        </form>
    </div>
</body>
HJP
  • 1
  • 1
  • 1
    = is assignment – epascarello Feb 28 '23 at 14:28
  • Use `==` or `===` to compare the value of two things. – Sean Feb 28 '23 at 14:29
  • btw, you need to take a single `'` inside of the event function call, like `` otherwise, you need to escape quotations. and please use loer case ltters for function and variables as start value (for instanciable function (using with `new`), take an upper case letter, to denote it (it does not change the functionality)). – Nina Scholz Feb 28 '23 at 14:32

0 Answers0