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

13th Dec 2017, 1:38 PM
ReimarPB
ReimarPB - avatar
4 Réponses
+ 1
x = eval($("#x").val()); y = eval($("#y").val());
13th Dec 2017, 1:51 PM
John Wells
John Wells - avatar
+ 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
13th Dec 2017, 1:51 PM
Cool Codin
Cool Codin - avatar
+ 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;
13th Dec 2017, 2:26 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 1
Thanks
13th Dec 2017, 1:58 PM
ReimarPB
ReimarPB - avatar