Although it said Way to go, but I think my code might be incorrect


#1



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


Well, exercise is done, no errors, but it shows an incorrect result.
Result: [ ',', '.' ]
I think the result will include my name... [Laynoooor]


/*jshint multistr:true */
var text = "Sen. Laynoooor, one of Donald Trump's most \
outspoken Republican critics, says he will not vote for either \
Trump or Hillary Clinton for president this year. Wrote by \
Laynoooor.";

var myName = "Laynoooor";

var hits = [];

for (var i = 0; i < text.length; i++) {
  if (text[i] === myName[0]) {
      for (var j = i; j < i + myName.length; j++);
      hits.push(text[j]);
  };
};

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


#2

it should, but your second for loop isn't correct. In your first for loop do things correctly:

for (loop){
   // code in the loop  
}

but then in your second for loop:

for (loop);

you decide to replace the {} which indicate what is inside the loop with a semi-colon


#3

Oops... I found my mistake.
And I wonder is that right if I add semicolon after every curly brackets, like the code below?
Thanks!

for (var i = 0; i < text.length; i++) {
  if (text[i] === myName[0]) {
      for (var j = i; j < i + myName.length; j++) {
      hits.push(text[j]);
      };
  };
};

#4

the closing curly brackets of for and if don't need a semi-colon:

for (var i = 0; i < text.length; i++) {
  if (text[i] === myName[0]) {
      for (var j = i; j < i + myName.length; j++) {
      hits.push(text[j]);
      }; // unnecessary semi-colon
  }; // unnecessary semi-colon
}; // unnecessary semi-colon

#5

If you copy and paste your code in, codecademyy labs:Link here It will show you some yellow triangle with what sort of error you made.

However, now if you put say Lincoln some where in your text it will not work. As you are considering only the first letter of your name and excluding the rest. You worte:


#6

True, with Lincoln it won't work, but the exercise never goes that far. We can put everything into one track


#7

Oh, yes you are correct actually this is not required for the exercise.


#8

I admit it would be a great extension to for example also check the last letter, but the and operator (&&) hasn't been covered yet, and this topic isn't about similar names like Lincoln