'Log It' Problem


#1

Hi, I have a problem with my code. I have edited my code and completed the exercise, however I the interpreter is not pushing my name to the variable 'hits' but it says it is 'undefined' instead. Can anyone have a look at this code and see where I have gone wrong. Thanks!

https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/6?curriculum_id=506324b3a7dffd00020bf661


/*jshint multistr:true */
var text = "Orison is one of the lowest\
developers ever. Orison cannot program unlike\
DishWashher that actually can.";

var myName = "Orison";

var hits = [];

for (var j = 0; j < text.length; j++) {
 if (text[j] === 'O') {
    // The 'for' loop below stops at each letter.
    // Remember k starts at 0, but incriments by 1 until
    // it reaches the length of "myName" variable.
    for (var k = j; k < (j + myName.length); k++) {
        hits.push(myName[k]);
        // 'j' is part of my name from "j < text.length".
    }
  }
}

if (hits === 0) {
    console.log("Your name wasn't found!");
} else {
    console.log(hits[k]);
}


#2

Everything looks good except you're trying to push letters from your myName variable instead of the text variable. So I'm sure the first mention of your name is logged but the second one is a bunch of 'undefined's because your myName string doesn't have indexed positions where you're searching (j - j + myName.length).

One thing, when looking at this exercise, what if your text contained the word "October"?


#3

First of all, thank you for helping me with this exercise. To answer your question:

1) I've made the changes you suggested, however it is still giving me the same error with the with the message 'undefined' as well as not giving me an array of letters (which also happened the first time). I'm not sue if I misunderstood your suggestion.

2) I can only find out if I'm somewhat able to correct my code; after that I can also check if including 'October' will work.


#4

Did you change it to hits.push(text[k]);?

Also, at the end of the exercise, just log hits without the[k].

The value of k at the end is likely much larger than hits has indexes of so that value will log undefined to the console.

Test it yourself, add the following to the end of your program and run it:

console.log(k, hits.length);

#5

Thank you very much for your help. To answer the 'October' question.

The compiler will take 'Octobe' with each letter having its own array instead of 'October'. I find it odd on my side.


#6

Yeah, exactly. I'm surprised they didn't take it one step further and had you:

  1. Detect 'O'
  2. Grab the next however-many letters are in your name and store it in a variable
  3. Compare that variable to your myName variable
  4. If they match, add the string to your hits array as a total word instead of one letter at a time.

#7

I'll definitely try this in my spare time at some point. Again, thank you for your help!


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.