It’s a Push

My daughter has been learning Javascript through Codeacademy’s online series of courses — ergo, I, too, have been learning Javascript. It’s been smooth sailing, more or less — we made a rock-paper-scissors game with no great difficulty, and we’re starting to wrap our minds around for loops and if statements.

Every once in a while, though, Codeacademy tells us to use a command without adequately instructing us on how to do that. And so, I turn to anybody still reading this moribund blog for an extra helping hand.

The command in question is push. I understand the basics of it — you use push to tack additional elements to the end of an established array. So if…

var dogs = ["poodle", "collie", "dachsund"];
dogs.push("chihuahua")

…then the array “dogs” will now be equal to poodle,collie,dachsund,chihuahua. Pretty straightforward, so far.

The task at hand is to search for my daughter’s name in a string of text. This has been challenging, but graspable: We have a for loop that checks characters, one at a time, in a block of text. When one of those characters matches the first character of Lea’s first name, it pushes it into an array (an array that starts off empty), and then grabs the next two characters and pushes those into the array as well, first one and then the other.

Except it’s not working.

Let’s look at the code.

var text = ("yak yak yak yak Lea bladi bladi bladi Lea bladi yakity yakity Lea yakkkkkkkkkkkkkkkk");
var myName = ("Lea");
var hits = [];

Okay, that establishes the variables — the block of text; Lea’s name; the empty array. Here’s the troublesome bit:

for (var m = 0; m < text.length; m++); { if (text[m] === 'L') { for (var n = m; n < m + myName.length; n++); { hits.push(text[n]); } } }

(Bah, WordPress won't let me do the indenting.)

So what's SUPPOSED to happen here is, when the mth character of the text matches L, the second for loop runs. That loop is supposed to push the nth character of the text into the array, plus the two that come after it. If I can check the value of a given character with text[m], then why can't I push that character with hits.push(text[n])?

Follow all that? Yes? Help?

I also can't figure out how to format an HTML web page so that when it encounters Javascript code, it runs it, so that we might take what we've learned out of the Codeacademy sandbox and try our luck in the real world. But that can be a problem for another day, I suppose.

Update: Already solved. We shouldn't have put semi-colons after the for statements -- it was as simple as that. Jeez. Thanks, Tyler!

Tweet about this on TwitterShare on FacebookShare on Google+Share on TumblrEmail this to someone
This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Michael
    Posted February 12, 2014 at 2:50 pm | Permalink

    Eric,
    You mention what is *supposed* to happen, which is clear, but you don’t mention what actually IS happening.
    Does hits end up looking like (“L”,”e”,”a”)? That is what I’d expect from the way ‘push’ works: as you say in the beginning, ‘push’ adds new elements to the array. Each new letter is considered a separate element.
    How are you testing the contents of hits[]? If you try a direct comparison of hits[] & myName, it will fail: one is an array of letters, the other an actual string.

    I have another approach in mind that would trap possible strings. Let me know if you’d like to hear it. (hint: whitespace).

       0 likes

  2. Eric Berlin
    Posted February 12, 2014 at 2:53 pm | Permalink

    Oh, well, good point — what happened was, CodeAcademy didn’t like the code, and said we were not successfully pushing anything into the hits array. But we’ve got it working now, thanks. It does indeed look like (“L”, “e”, “a”), which is unattractive but all CodeAcademy requires from us, so, we’re good.

       0 likes

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  • Archives

  • Subscribe

    By signing up, you agree to our Terms of Service and Privacy Policy.

  • Twitter