3

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>
Weafs.py
  • 22,731
  • 9
  • 56
  • 78
Melody Hajian
  • 275
  • 1
  • 3
  • 12
  • 2
    The values you read at the start are strings. to get numbers, use `parseInt()` or `parseFloat()`. – Sirko Sep 02 '14 at 07:11
  • 2
    possible duplicate of [how to sum two numbers from input tag?](http://stackoverflow.com/questions/11961474/how-to-sum-two-numbers-from-input-tag) – Viktor Bahtev Sep 02 '14 at 07:11
  • 1
    @ViktorBahtev dude i didn't even know what is the problem. – Melody Hajian Sep 02 '14 at 07:14
  • possible duplicate of [How to add two strings as if they were numbers?](http://stackoverflow.com/questions/8976627/how-to-add-two-strings-as-if-they-were-numbers) – dcorking Sep 02 '14 at 07:23
  • @MelodyHajian you wrote "i didn't even know what is the problem." Don't worry, the 'duplicate' flag is not a criticism of your question. Instead, the link from your question to the canonical question allows users to answers that apply to both questions. – dcorking Sep 02 '14 at 08:23
  • Drat - I can't edit my comment. I meant to write: "allows users to _find_ answers that apply to both questions" – dcorking Sep 02 '14 at 08:53
  • http://stackoverflow.com/help/duplicates – dcorking Sep 02 '14 at 08:57

6 Answers6

2

You may use like this:

z= +x + +y; // + is prefixed to convert input into number
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
2

The x and y variables contain strings. Parse them to numbers:

var x = parseFloat(document.getElementById("inp1").value);
var y = parseFloat(document.getElementById("inp2").value);

It happens to work for the other operators, because there are no subtraction, multiplication or division for strings, it figures out that it has to convert the strings to numbers.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1
var x=document.getElementById("inp1").value;
var y=document.getElementById("inp2").value;

return you values in those text boxes as strings.

When you use + operator on strings, it will concatenate the values. If you use the same operator on numbers, it will add the values.

You will need to parse the text box values into integer using the parseInt function using one of the following ways.

var x=parseInt(document.getElementById("inp1").value);
var y=parseInt(document.getElementById("inp2").value);

and then do z=x+y; I would recommend this because all the operations, not just addition, will be perfomed on the integers.

or simply change z=z+y; to look like z = parseInt(x) + parseInt(y);

AdityaParab
  • 7,024
  • 3
  • 27
  • 40
1

A quick way to convert a string to a number is to use the unary + operator.

z = +x + +y

or

z = parseInt(x) + parseInt(y);
Rahul K
  • 413
  • 3
  • 11
1
var x = parseInt(document.getElementById("inp1").value);

this converts your "string" number to integer, also you can use parseFloat() if you have float numbers

CMS
  • 704
  • 3
  • 9
  • 32
1

You may use like this:

var x = document.getElementById("inp1").value*1;
var y = document.getElementById("inp2").value*1;

x in this moment is number!!

More clean for me!!!

Seconddj
  • 9
  • 2
  • 1
    This is the only answer with `*1`. What does it do? (I guess it multiplies the string by 1 and so converts it to an integer.) Since you recommend this approach (and I agree that it looks nice), I suggest you post it as an answer to the canonical question http://stackoverflow.com/questions/8976627/how-to-add-two-strings-as-if-they-were-numbers – dcorking Sep 02 '14 at 08:17
  • 1
    when multiplied by 1 (* 1), is converted into a number, not an integer, it is different. – Seconddj Sep 17 '14 at 07:57