Help! Why this code not work!? What wrong is it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Help! Why this code not work!? What wrong is it?

I'm prompting user to enter string for username. But if they press cancel, it gets sent as null. How do I make it change to a default username ("Steve") if they press cancel/if it returns null? var myUsername = prompt("Enter username", "Steve"); document.write(myUsername); Please see code and tell me what I did wrong? https://code.sololearn.com/W9T03UNv5jcH/?ref=app

6th Jun 2018, 10:57 PM
Andre Daniel
Andre Daniel - avatar
3 Answers
+ 8
replace your code with this 👇 var myUsername = prompt("Enter username", "Steve"); if(myUsername == null) myUsername = "Steve"; document.write(myUsername);
6th Jun 2018, 11:07 PM
Mohammad Amir Aqeel
Mohammad Amir Aqeel - avatar
+ 7
My Pleasure !!!😊😊 Andre Daniel
6th Jun 2018, 11:15 PM
Mohammad Amir Aqeel
Mohammad Amir Aqeel - avatar
+ 1
It worked! Thank you Mohammad Amir Aqeel 😊
6th Jun 2018, 11:14 PM
Andre Daniel
Andre Daniel - avatar