Mehmet
Mehmet

Reputation: 227

Javascript calculator addition is not working properly

Here is my really basic Javascript calculator code. Except for the addition symbol, my calculator is just working fine. However, addition is not doing its job: instead it combines them. How to fix it?

var first = prompt("First number");
var second = prompt("Second number");
parseInt(first);
parseInt(second);
var islem = prompt("Is it +/-/* or /?");
if (islem == "+" ) {
    document.write(first + second);
}
else if ( islem == "-") {
    document.write(first - second);
}
else if ( islem == "*" ) {
    document.write(first * second);
}
else {
    document.write(first / second);
}

Upvotes: 2

Views: 380

Answers (5)

daniel.rigberg
daniel.rigberg

Reputation: 128

These answers are all great! I'd go one step further and

1) Call parseInt() on the prompts immediately, so that you're not declaring as many unnecessary variables

2) Use a switch statement instead of an if/else! It'll make it easier to add other cases, for exponentiation, etc. This one's more of a style choice, though :)

3) You can also consider using parseFloat() instead of parseInt() so that it can handle decimal inputs!

var first = parseInt(prompt("First number"));
var second = parseInt(prompt("Second number"));
var islem = prompt("Is it +/-/* or /?");
switch (islem) {
    case "+":
        document.write(first+second);
        break;
    case "-":
        document.write(first-second);
        break;
    case "*":
        document.write(first*second);
        break;
    case "/":
        document.write(first/second);
        break;
    default:
        document.write("Excuse me? Give me an operator, please!");
};

Upvotes: 2

Traveling Tech Guy
Traveling Tech Guy

Reputation: 27811

You are not storing the parseInt result anywhere, and instead performing operations on strings. Since + is also a string concatenation operation, you get the concatenated string as a result. Try changing lines 3-4 to:

first = parseInt(first, 10); //don't forget the base!
second = parseInt(second, 10);
//TBD: add some error handling code to check values

Upvotes: 4

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48357

The parseInt() function parses a string argument and returns an integer.

You have to assign parsed value to your variable, like this:

first=parseInt(first);
second=parseInt(second);

Or simply,

document.write(parseInt(first)+parseInt(second));

See reference here.

var first = prompt("First number");
var second = prompt("Second number");
parseInt(first);
parseInt(second);
var islem = prompt("Is it +/-/* or /?");
if (islem == "+" ) {
    document.write(parseInt(first)+parseInt(second));
}
else if ( islem == "-") {
    document.write(first-second);
}
else if ( islem == "*" ) {
    document.write(first*second);
}
else {
    document.write(first/second);
}

Upvotes: 4

Scott Marcus
Scott Marcus

Reputation: 65806

When you use parseInt(), you need to capture the returned value so that you can use that number going forward. Right now, you are calling parseInt() and not doing anything with the returned value, so it is lost immediately.

It is also advisable to use the optional second parameter to parseInt(), which is the radix value that sets the base of the number you are working with. Typically, you'll want that to be 10 when working with base 10 numbers. This is really important because if the string you are working with starts with a 0 and you don't supply the radix, the operation will assume you are working with an octal value and if the string were to start with 0x, then the operation will assume you are working with a hex (base 16) value.

See more about using parseInt().

The code should be:

var first = prompt("First number");
var second = prompt("Second number");
first = parseInt(first, 10);
second = parseInt(second, 10);

Upvotes: 4

Deeksha Pandit
Deeksha Pandit

Reputation: 74

Here is the right program:

var first = prompt("First number");
var second = prompt("Second number");
var firstParsedInteger = parseInt(first);
var secondParsedInteger = parseInt(second);
var islem = prompt("Is it +/-/* or /?");
if (islem == "+" ) {
    document.write(firstParsedInteger + secondParsedInteger);
}
else if ( islem == "-") {
    document.write(first-second);
}
else if ( islem == "*" ) {
    document.write(first*second);
}
else {
    document.write(first/second);
}

Note: You were never using the parsed value. In javascript, if you are using var for numbers and using '+' operator. this treats those numbers as string not as a number. Remember to parse integer when playing with var and + in javascript.

Upvotes: 2

Related Questions