Pramito Rahman
Pramito Rahman

Reputation: 97

How to add two numbers?

I wrote a JavaScript calculator... but suppose when I give my first number as 2 and the second number as 3, the result is showing 23, but I wanted to add the two numbers.

Can anyone please help me? It is also happening when I try to minus the two numbers. Why isn't this working?

var cal = prompt("Please enter what type of calculation you want to do\n
if you wanna add enter = 1\n
if you want to minus enter = 2\n
if you want to divide enter = 3\n
if you want to multiply enter = 4");

if (cal == 1) {
    var a = prompt("Please enter your first number");
    var b = prompt("please enter your second number");

    alert("The result is , " + a+b);
}

if (cal == 2) {
    var c = prompt("Please enter your first number");
    var d = prompt("please enter your second number");

    alert("the result is , " + c - d);
}

Upvotes: 7

Views: 58473

Answers (11)

zeeshan
zeeshan

Reputation: 1

num1 = window.prompt("Please Enter Your Num1 :");
num2 = window.prompt("Please Enter Your Num2 :");
var sum = parseInt(num1)+ parseInt(num2);
document.writeln("<h1> The summ of two integers,"+num1+", and,"+num2+", is ,"+sum+",</h1>"  )

I Hope this will help you.

Upvotes: 0

Gregory Nozik
Gregory Nozik

Reputation: 3374

I think it happens because var is dynamic type variable, so your variable translated as string.

Please note that function can receive double, so parseInt may not work correctly

function Add(a,b)
{
   var result = null;

   if (isNaN(a) || isNaN(b))
   {
      alert("Please send the number");
      return;
   }

   a = Number(a);
   b= Number(b);

   return a+b;
}

Upvotes: 0

Vijendra Singh
Vijendra Singh

Reputation: 628

The Prompt method returns the entered value as a string.

So after Prompt use parseInt(), this function parses a string and returns an integer.

Upvotes: 1

Saiyam
Saiyam

Reputation: 138

Use:

var add = parseFloat(a)+parseFloat(b);

Upvotes: 0

Satpal
Satpal

Reputation: 133403

Try this:

var cal = prompt("Please enter what type of calculation you want to do\n" +
  "if you want to add enter = 1\n" +
  "if you want to minus enter = 2\n" +
  "if you want to divide enter = 3\n" +
  "if you want to multiply enter = 4");

if (cal == 1) {
    var a = prompt("Please enter your first number");
    var b = prompt("please enter your second number");

    alert("The result is , " + (Number(a) + Number(b)));
}

else if (cal == 2) {
    var c = prompt("Please enter your first number");
    var d = prompt("please enter your second number");

    alert("the result is , " + (Number(c) - Number(d)));
}

Upvotes: 2

PleaseStand
PleaseStand

Reputation: 32052

The binary + operator has two uses: addition and string concatenation. Although you want the former, the latter is happening because window.prompt() returns a string.

To avoid this, you should do one of the following (read the documentation so you can understand the differences):

It is wise to check whether the numbers could be parsed (using isNaN(num), or possibly num === num) before trying to perform calculations with them, so your script can show a helpful error message instead of just carrying NaN through to its output.

Upvotes: 1

Tamil Selvan C
Tamil Selvan C

Reputation: 20199

Use

alert("The result is , " + (parseFloat(a)+parseFloat(b)));

Upvotes: 0

Michael Robinson
Michael Robinson

Reputation: 2127

Prompts return strings, you need to parse them as integers (you could also do floats using parseFloat)

alert("The result is , " + (parseInt(a) + parseInt(b)));

Upvotes: 1

zerkms
zerkms

Reputation: 254886

Convert a string to a number after you accept it from user:

a = parseInt(a, 10);

Upvotes: 1

Danny Beckett
Danny Beckett

Reputation: 20850

The + sign is used to concatenate strings together, not to add them together mathematically.

You need to wrap your variables in parseInt(), e.g.

alert("The result is , " + parseInt(a)+parseInt(b));

Upvotes: 1

user177800
user177800

Reputation:

You are adding to Strings; known as concatenation, you need to convert the String representations to numbers then add them.

alert("The result is , " + (parseInt(a) + parseInt(b)));

Upvotes: 0

Related Questions