Log it! problem


#1


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


The site says "Syntax Problem"


 /*jshint multistr:true */
var text = "Shrike rhjdl;afajd;va'afojvbamld,ng;afvl;acb'a";
var myName = "Shrike";
var hits = [];
  for (i=0; i<text.length; i++) {
      if (text[i]===myName[0]) {
          for (j = i; j=i+myName.length) {
              hits.push(text[j]);
          }
      }
  }
  if(hits.length === 0) {
    console.log("Your name wasn't found!");
} 
else {
    console.log(hits);
}


#2

If j = i, j will never equal i+myName.length. Also, where is the expression in your for loop?


#3

j= i is just where the loop starts. What do you mean by "expression in my for loop"?


#4

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration

for (j= i ; this part tells the loop where it's starting, the 'initializer'

j= i+myName.length; this part must be true for the loop to run, the 'condition'

The expression is the part that tells the loop what the next value to check for will be.

In your first loop, the expression is i++. So that loop starts scanning from the 1st letter in text to the next, etc. almost all of the way through the characters in that variable. Since your 2nd loop is starting from j, your increment expression should be j++.


#5

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