cropped-photo-1430165558479-de3cf8cf1478.jpg

typeof();

this javascript code:
 var weird = [1, 40, "bob", [], false, 89];
 var count = 0;
 for (var i = 0; i < weird.length; i++) {
  if (typeof(weird[i]) === "number") {
    count += weird[i];
  }
 }

number needs to be in quotes here or it doesn’t work.  That surprised me because I thought that would make it a string of the word number.    I tried putting
typeof(1);
into the console and it returned
“number”
So I guess that makes sense.  It just surprised me.  I guess because I’m asking to compare the typeof it would never return the value of a string, just what type.  It still seems strange that it should be in quotes but there you go!

cropped-photo-1430165558479-de3cf8cf1478.jpg

Notes to self about learning to program

I’m going to start putting little things that were unclear to me before I have an a-ha! moment. This blog is about my journey learning to code so some of these will almost certainly be obvious to some people, but hopefully if someone else who is learning searches for an answer finds  my post, well, good for me right? And more to the point good for the person whose question is answered! So here’s the first little a-ha.

I’m learning javascript so right now I’m learning about changing text and html in a document.

  • Input fields like ‘values’
  • HTML elements have ‘innerHTML’

In other words, what’s in a <div> is it’s innerHTML but what’s in a text box is its value.

I know, probably remarkably simple and obvious to some folks but hey, it was something that briefly gave me a problem and I figured out so here you go.