(JavaScript) outputs string instead of int | SoloLearn: Learn to code for FREE!


(JavaScript) outputs string instead of int

I have a problem. I made this code to output the sum of 2 numbers but instead it gives me a string. How do you fix this? https://code.sololearn.com/WRs88DL05i1v/?ref=app

12/13/2017 1:38:12 PM


4 Answers

New Answer


x = eval($("#x").val()); y = eval($("#y").val());


x = parseFloat($("#x").val()); y = parseFloat($("#y").val()); Use parseFloat to parse the string to float cuz the user can also enter numbers with decimals


//For avoid method evaluated inside eval args.such as eval("alert()") x=$("#x").val()-0; y=$("#y").val()-0; --or-- x=+$("#x").val(); y=+$("#y".val(); --or-- sum=x*1+y*1;