Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The adding up button doesn't work in javascript

Tags:

javascript

I made a simple calculator in javascript but the + button doesn't work and it just show the numbers near together

Here is my code:

<script>
function calc(operator) {
    var x = document.getElementById("inp1").value;
    var y = document.getElementById("inp2").value;
    var z = 0;

    switch (operator) {
        case "+":
        z = x + y;
        break;
        case "-":
        z = x - y;
        break;
        case "*":
        z = x * y;
        break;
        case "/":
        z = x / y;
        break;
    }
    document.getElementById("result").innerHTML=z;
}
</script>
like image 429
Melody Hajian Avatar asked Jun 01 '26 23:06

Melody Hajian


1 Answers

You may use like this:

z= +x + +y; // + is prefixed to convert input into number
like image 145
Bhojendra Rauniyar Avatar answered Jun 04 '26 11:06

Bhojendra Rauniyar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!