Hello every1,
I want to take two integer value from the user and display the result(sum) as output..
I am using the following JS code :

function message_comm()
{
var num1=prompt("Enter the 1st number");
var num2=prompt("Enter the 2nd number");
alert('The sum :'+num1+num2);
}

The output I am currently obtaining is a concat-string of the two input values
Ex:
Prompt for num1 ,I enter value 5
Prompt for num2 ,I enter value 5
The result obtained(at current) is 55...:(
I want the answer to be displayed as 10
..

Thanking every1 in advance
Thank You

Recommended Answers

All 4 Replies

You can convert the string to integer by...

alert('The sum :'+parseInt(num1,10)+parseInt(num2,10));

@ Taywin I just tried replacing the line of code with the one

alert('The sum :'+parseInt(num1,10)+parseInt(num2,10));

still no change result obtained is a concat-string:(

You can convert the string to integer by...

alert('The sum :'+parseInt(num1,10)+parseInt(num2,10));

Thank you, very much now it works fine you forgot to put extra pair of brackets

alert('The sum :'+(parseInt(num1,10)+parseInt(num2,10)));

Thank you again

Oh sorry about the missing parenthesis. :P Anyway, you now know how to convert a string to integer before you use it. :) Please mark it solved too, so it would not get revived in the future as unsolved. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.