+ 1

Why does this function come out as NaN?

Don't worry, the function itself has little TypeScript in it. I'm making a function that adds 2 numbers together, if the parameters aren't numbers, it converts them to numbers. I tried using isNaN() but it still isn't returning a value. Line 48. https://code.sololearn.com/WGdV8Fhu7yjs/?ref=app

28th Jul 2020, 9:15 AM
Clueless Coder
Clueless Coder - avatar
5 Antworten
+ 2
Well, the first argument you passed to it was "one". How was that supposed to pass the `isNaN` check?
28th Jul 2020, 9:46 AM
Ipang
+ 2
on line 50 you are converting "one" to int so it will print NaN because "one" cannot be converted to integer.. If your string was something "1one" then certainly it would extract 1 from the string and convert it to int..
28th Jul 2020, 10:06 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 1
Which one, the `add` function or `add2`? I think both are working as they should, what were your expectation?
28th Jul 2020, 9:38 AM
Ipang
28th Jul 2020, 10:10 AM
Clueless Coder
Clueless Coder - avatar
0
Ipang I said line 48, so add2(). I was expecting it to convert any string to a number and add them. Instead it returns NaN
28th Jul 2020, 9:43 AM
Clueless Coder
Clueless Coder - avatar