Prompt User for Name

So I started working on my first goal: Prompt User for name

Even though this was suppose to be the easiest one, I still didn't know where to begin. So I wrote down what exactly I needed in order to prompt user for their name:

  • when the page loads I want an alert to come up ( HTML && JavaScript )
  • inside the alert I want to ask the user for their name. ( JavaScript )
  • when the user types in their name I want to store the information for later( JavaScript )
So from a lesson from Village Park, I know that there are Event Handlers, like onclick, which we did so that when you click on the text, something happens. This is done through a function.

The example we got for this was on an onclick the background colour would change from white to red. First we have to create a place to click on the browser that will change the colour when it is clicked. Everything that is seen on our browser needs to be called in the HTML:

    <span onclick= "changeBgRed();"> Red</span>

Here we call the function "changeBgRed()" from JavaScript . This means we need to make the function in Javascript that will change the background color:
    function changeBgRed(){
      document.bgColor = "red"
    }

Great, that doesnt seem too difficult! So I want to have a instead of an onclick event handler, I want to create an onload one(this is like identifying the cat! I'm good!) So in my HTML I put:

    <span onload= "getName()"></span>

And now to write my function. This became more like identifying the tiger, now I have find out what the persons name is and store it so I can use it again in the future! So first, I have to create a variable for name:

    var name = '';

This will be a global variable called outside my function so that I can use it again. But how do we ask people for their name? I remembered that during my lessons with CodeAcademy, they loved prompting for stuff! So I went back to my notes on Slaying a Dragon to remind myself how to prompt a question.

    function getName(){
      name = prompt("Hey new friend! What's your name?");

From here we get a working alert! Whenever we load a page it will ask you for your name. Yes! This was a huge victory. But what if someone doesn't put in their name? Well that is just not cool! So I want to create an if/else statement that says if nothing is typed into the prompt, then we ask them again! (Polite, but persistant!)

Now, there is something majestically evil about JavaScript. Rather than telling me what is wrong, just something doesn't work, such as the prompt keeps prompting or things that use to exist don't anymore. So in this case, once I entered my name in the prompt, it would refresh and ask for my name again, and again, and again. Sometimes it would stop with asking three times sometimes it would be infinite (I’m not sure why but like I said, JavaScript is evil). After staring at my code for 30 minutes wondering how this if/else statement could possibly be wrong and trying to write it in another format that might please the beast(JavaScript), I decided to block it out and make sure the onload was still working or if I had done something more damaging than I thought and it had nothing to do with my if/else statement. So I blocked out my code and refreshed the page. Luckily, only one prompt statement came up and I gave a sigh of relief. This was around midnight and I was really happy that there wasn’t a more complex problem with my chat, I began typing my thoughts into my chat box, with a smirk of satisfaction that my issue was probably minor and I could fix it before bed. Then to my horror, my thoughts began to taunt me, literally.

I was freaking out. Nothing like this had happened. To anyone. Ever.

Actually I don’t know that, but I hadn’t seen it before and what started with just repeating my previous line, quickly morphed into a more complex being. It could repeat one line twice on the same line, and it started picking words on its own rather than the entire line. I was panicking- it was learning faster than I was and becoming smarter. As my chat literally began spewing shit at me, my logic finally kicked in: My chat can’t possibly be this smart! I remembered I’d told my friend that I had created this chat and I guess right at the moment it was repaired, and I was typing my stream of consciousness, he got on and was just playing with me.

This was HILARIOUS! I was so tired and had been staring at my computer for over 13 hours. I couldn’t stop laughing, I had been so hyper focused on this script for so long that I wasn’t being logical. When the chat was repeating stuff I was really losing it, in my state of panic I was drawing the most obscene conclusions, and now that I had a minute to breath I realized how insane those thoughts really were. So this practical joke kind of reset my brain and gave me the much needed break I had been putting off for hours.

I finally saw what might be making it so unhappy: I was missing a semicolon at the end of my alert… seriously. So I added in the semicolon, crossed my fingers and refreshed the page…

”It WORKED!”

I yelled, to myself, in my bed at almost 1 am. My victory arms flew up as I watched my first truly successful if/else statement get executed. Relief, satisfaction and pure glee was what I felt. As I came down from this strange high… I realized that I had learned two valuable insights from this long day. First, take breaks, my brain needs it! Secondly (and probably more importantly), my chat definitely needs to have the users name beside their message!

comments powered by Disqus