DEV Community

Cover image for You won't be able to answer this simple JavaScript question!

You won't be able to answer this simple JavaScript question!

Sadeedpv🥇 on August 16, 2023

Last night, I was scrolling through twitter and I found a interesting JavaScript question, and I bet you won't be able to answer this without cheat...
Collapse
 
unfor19 profile image
Meir Gabay

I wanted to see if ChatGPT will get it right.. And it did :)

The NaN part is very interesting, as one of the "a" chars is dropped, initially, I didn't understand why it's not "bananaa" (extra "a"), now I get it 💪🏻

Image description

Collapse
 
vsaulis profile image
Vladas Saulis

banana

Collapse
 
bob4262 profile image
Bob dnaeil

What!! No wayy, how is it banana

Collapse
 
bob4262 profile image
Bob dnaeil

Where does the n come from??

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

Unary plus on "a" gives you NaN

Collapse
 
equiman profile image
Camilo Martinez

Now try console.log(new Array(4).join(Number('y tho') + 'a')+' Batman!') 🦇

Collapse
 
sadeedpv profile image
Sadeedpv🥇

NaNaNaNaNaNa batman! is that the desired output or am I missing something? 🤔

Collapse
 
equiman profile image
Camilo Martinez

Yes, maybe for people from my generation 😅.

Now I think this joke is too old 👴

Thread Thread
 
sadeedpv profile image
Sadeedpv🥇 • Edited

Haha, maybe that joke is from the 'dad joke' era! 😄😄.

Collapse
 
lexlohr profile image
Alex Lohr

NaN is actually part of IEEE 754, which is used in most languages to express floating point numbers.

Collapse
 
skyops117 profile image
SkyHops • Edited

"baaa"

Collapse
 
sadeedpv profile image
Sadeedpv🥇

Its actually banana, basically the unary plus(++) of 'a' gives NaN which gives us 'baNaNa' and convert it to lowercase => banana

Collapse
 
skyops117 profile image
SkyHops • Edited

Do you need

"use strict" 
Enter fullscreen mode Exit fullscreen mode

at the top of the script 🤔
I used an online Javascript console.

Thread Thread
 
sadeedpv profile image
Sadeedpv🥇

Try this code :

console.log(("b" + "a" + + "a" + "a").toLowerCase());
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
skyops117 profile image
SkyHops

Thank you; my mistake.
I didn't see the ++.
For my defence on my phone it red:

("b" + "a" +
 + "a" + "a").toLowerCase()
Enter fullscreen mode Exit fullscreen mode
Collapse
 
chrdek profile image
chrdek

yep, that' s what it does.

Collapse
 
kweisvee profile image
Chrissy Vinco

Okay the person who thought of this problem is soooo clever

Collapse
 
shiyulong11 profile image
Dragon

JavaScript is really a magical language

Collapse
 
vsaulis profile image
Vladas Saulis

when it gets to insane questions...

Collapse
 
khair_al_anam profile image
Khair Alanam

That's honestly cool.

baNaNa

Collapse
 
sadeedpv profile image
Sadeedpv🥇

and weird

Collapse
 
bob4262 profile image
Bob dnaeil

Is it bAaa

Collapse
 
codeystein profile image
codeyStein

It's the little things that makes javascirpt so annoying :)

Collapse
 
intermundos profile image
intermundos

In your case it's bananaa

Collapse
 
sadeedpv profile image
Sadeedpv🥇

It's banana

Collapse
 
bosshie96 profile image
BOSSHIE • Edited
console.log(("b" + "a" + + "a" + "a").toLowerCase());
Enter fullscreen mode Exit fullscreen mode

banana

Collapse
 
sadeedpv profile image
Sadeedpv🥇

✅✅

Collapse
 
overflow profile image
overFlow

now i see why typeScript and them exist.
obviously it is string :"b"+string :"a"+NaN+string :"a".
Do not ask me about the last string :"a".
let us say it is your homework.

Collapse
 
nadirhusaain profile image
Nadir Hussain

banana

Collapse
 
jburky15 profile image
Joe Burkhart • Edited

It took me a second because I didn't notice the ++ (Unary plus) right away. Definitely got a little laugh out of me once I caught it though.