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 )
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>
function changeBgRed(){
document.bgColor = "red"
}
<span onload= "getName()"></span>
var name = '';
function getName(){
name = prompt("Hey new friend! What's your name?");
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