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

0

(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

ReimarPB

4 Answers

New Answer

+1

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

+9

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

+5

//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;

+1

Thanks