...

View Full Version : Turning a string var to num



hcinderela
04-28-2008, 06:54 PM
I have a simple problem. I need the variables that I am collecting to add as numbers, but instead they are concatenating as strings. How can I make them numbers?



function disp_prompt(){
var num1=prompt("Please enter a number","");
var num2=prompt("Please enter another number","");
parseInt(num1);
parseInt(num2);
var sum=num1+num2;
document.write("You entered: ",num1," and ",num2,"<br>");
document.write("Sum: ",sum);


As you can see I tried parseInt(), but that doesn't seem to work. Thanks.

mjlorbet
04-28-2008, 06:55 PM
parseInt(num1);
parseInt(num2);
var sum=num1+num2;



should be


var sum = parseInt(num1) + parseInt(num2);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum