Why is this code displaying NaN as output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why is this code displaying NaN as output?

https://code.sololearn.com/WthMfWpY8df6/?ref=app

3rd Mar 2019, 6:00 AM
Ghost rider
6 Answers
+ 6
function multiply() should not have those parameters. Remove them. The reason for NaN is that you were using the variable a and b in the function scope (those are not from the prompt) . As those variables haven't been initiazed the expression happens like this. var c = a*b = undefined*undefined = null
3rd Mar 2019, 6:12 AM
Seniru
Seniru - avatar
+ 6
Also, var c = a*b instead of var c = document.write(a*b);
3rd Mar 2019, 6:33 AM
Anna
Anna - avatar
+ 4
Coder What's the advantage of storing the call to document.write in a variable? Doesn't make sense to me 🤔
5th Mar 2019, 12:21 PM
Anna
Anna - avatar
+ 2
Satya Routray Anna said that because c will store undefined as document.write method returns nothing instead of a*b as you want.
3rd Mar 2019, 7:53 AM
Seniru
Seniru - avatar
+ 1
Nothing… And I don't think this is going to work… it'll return undefined😀 edit: What is the use of var c? (after you edited) Scrap it. Coder
5th Mar 2019, 12:31 PM
Emoji FanBoy
Emoji FanBoy - avatar
0
Anna why ?
3rd Mar 2019, 7:46 AM
Ghost rider