0

Not working

please help why is it not adding numbers https://code.sololearn.com/WkC78F1kW4Sp/?ref=app

21st Sep 2017, 3:12 PM
vansh
vansh - avatar
6 odpowiedzi
+ 6
This will make your code to add numbers var result= num1*1+num2*1;
21st Sep 2017, 3:59 PM
Siddharth Saraf
+ 5
Thanks @visph, I would've marked your answer as best if I've got the power, but ughhh :D
22nd Sep 2017, 9:53 AM
Dev
Dev - avatar
+ 3
work fine, but prompt get a string. You need to convert it to number
21st Sep 2017, 3:19 PM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
+ 3
@Dev: eval() is to be avoided, as it's relatively unsafe, and almost there's other implicit and explicit ways to convert string to numbers: > writing an expression where cast is implicit (as suggested by @Siddhart Saraf): var result = num1 + num2; ... will produce string if any of num1 or num2 is string, but number if both are number: as * operator id done before + operator, multply by one will just implicitly cast the variable to number; but (+num) will do same job, with a few less cpu usage than doing multiplication (while there's no compiler/interpreter optimization taking in account that *1 has not to be really computed): var result = (+num1)+(+num1); (parenthesis are required to start by converting to number, and so get an implicit number result > writing explicit cast: num = parseInt(str); num = parseFloat(str); num = Number(str); ... so you can use it (them) inlined: var result = Number(num1)+Number(num2) Differences between parseInt() and parseFloat() is obvious, but less with Number(): Number() will convert to integer or float value according to context, and is a few more restrictive than other ones in accepting values as valid: Number('42text') === NaN; parseInt('42text') === 42;
22nd Sep 2017, 9:44 AM
visph
visph - avatar
+ 3
@Dev: maybe asker will do it... or not ;P
22nd Sep 2017, 9:56 AM
visph
visph - avatar
0
thank you @visph
22nd Sep 2017, 9:57 AM
vansh
vansh - avatar