0

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

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.Also, please do not use "chat speak".

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Taywin
0

You can convert the string to integer by...

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

@ 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:(

0

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

0

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. :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.