chrismartucci

(Because I needed a hobby)

I'm learning JavaScript, and I helped someone

Posted January 21, 2014

I have a "Someday/Maybe" list, on which I wrote, "Learn to code software" (also, "Dungeons and Dragons?"). One month ago, I stumbled upon Codecademy and thought it a brilliant opportunity. So I created an account and began my journey.

I started with the "Web Fundamentals" lessons, although I already knew a good deal of HTML and CSS. Still, I learned a few new tricks, including proper use of CSS positioning (apparently you're not supposed to just randomly try block, inline, and inline-block until something looks right). But it was JavaScript that I was truly excited to learn--something I'd never written before.

As it turns out, writing code is an experience I enjoy. Though I cannot say it has come "naturally" for me, I do believe I am picking things up nicely. Last week, my wife, Elizabeth, called me:

E: Hey, you think you can help me with something?
Me: Yeah?
E: So this graphic designer is creating a website for a dentist in Brooklyn, and she wants the greeting text on the homepage to change every time you refresh the page. Is that done with CSS, or JavaScript, or something?
Me: I don't think you can do that with CSS. Maybe. You can definitely do it with JavaScript.
E: Well good, because I told her you could code it.

Well alright then.

Fortunately for us, what the graphic designer was trying to accomplish was relatively simple, and I had actually learned enough about random numbers and arrays to whip something up. Now it might not seem like a big deal to some, but I was extremely proud of this:

<div style="font-family: verdana, sans-serif; font-size: 42px; font-weight: bold; color: blue;">
<script type="text/javascript">
var greeting = ["Hello", "Bonjour", "Greetings", "Hi there"];
var random = Math.floor(Math.random() * 4);
document.write(greeting[random]);
</script>
</div>

Boom. It worked. I emailed the code to the designer, explaining how she could customize the styling, add her own greetings, and how she had to change the number "4" to the number of items in the array. Indeed, I felt like a champion. I told Elizabeth how much fun it was, and she encouraged me to continue learning, which I certainly will.