JamesThePanda Posted November 17, 2009 Share Posted November 17, 2009 hi guys Im realy new to learning jscript basically I want to make a litle script with 2 propts asking for 2 numbers and the it adds them both together the script I made is as follows <script type="text/javascript"> function add(a,b) { x=a+b; return x; } var num1=prompt("what is number a?"); var num2=prompt("what is number b?"); alert(add(num1,num2)); </script> I entered 1 for both the number and the ouput comes out as 11 can someone point out were im going wrong. I wanted the out put to be 2 thanks james Link to comment https://forums.phpfreaks.com/topic/181830-solved-noob-question-regading-varibles/ Share on other sites More sharing options...
ngreenwood6 Posted November 17, 2009 Share Posted November 17, 2009 The reason it is doing that is because you are adding 2 strings together. You need to convert those strings to integers. Try this: <script type="text/javascript"> function add(a,b) { x=parseInt(a)+parseInt(b); return x; } var num1=prompt("what is number a?"); var num2=prompt("what is number b?"); alert(add(num1,num2)); </script> Link to comment https://forums.phpfreaks.com/topic/181830-solved-noob-question-regading-varibles/#findComment-958920 Share on other sites More sharing options...
JamesThePanda Posted November 17, 2009 Author Share Posted November 17, 2009 I knew it was returning them as a string, just didnt know how to make it a interger Thanks again Link to comment https://forums.phpfreaks.com/topic/181830-solved-noob-question-regading-varibles/#findComment-958921 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.