Search text for your name


#1

Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable.
This is the error that has given me. I'm stucked, I don't know where is my mistake and I am confused very much. Can you explain the purpose of these, "var hits = ...." and "for loops" in this exercise? Thank you.

/*jshint multistr:true */
var text = "Hey Rabia, how are you? sfslfg slsdju \
jkdsgkf aswes tsawsdred \
asdfdtryyerg sdfte sdsrw, Rabia?"
var myName = ("Rabia");
var hits = [ ];
for (var i = 0; i < text.length; i++) {
if (text[i] === "R"); {
for ( j = i; j < i + myName.length; j++) {
console.log(j)
console.log(text[j])
}
}
};


#2

the first for loop loops over the text looking for the first letter of your name (if condition), if the first letter is found, the second loop will loop over the letters of your name, then you can push them into the hits array so that in the end you have an array with all instances of your name from text


#3

Within your second for loop it wants you to push each letter starting with the first "R" it finds and ending with the last letter of your name. Below I will copy and paste your code and simply add comments above each section explaining what each part does and then add the part that's missing:

/*jshint multistr:true */
var text = "Hey Rabia, how are you? sfslfg slsdju \
jkdsgkf aswes tsawsdred \
asdfdtryyerg sdfte sdsrw, Rabia?"
var myName = ("Rabia");
var hits = [ ];

// The for loop below will cycle through each letter of the string myName until it reaches the end of the text.
for (var i = 0; i < text.length; i++) {
   // This part is obvious, as it checks if the current letter(position i) matches "R".
  // YOU NEED TO REMOVE THE SEMICOLON BEFORE THE OPENING CURLY BRACKET RIGHT HERE.
   if (text[i] === "R"); {
      // Now this for loop begins if it finds an "R" in the string. The loop starts by putting j equal to i and then uses 
      // j to mark the position in the string we want to start at, which should be right on the "R" it found.
      for ( j = i; j < i + myName.length; j++) {
         // The line below will push the current position in the array text(which is j) to the next empty spot in the array
         // hits. It will do this each loop, and each time it loops j will increase by 1, and will loop again for each letter in your name after "R". This will push the next 4
         // letters after "R" as well and then stop pushing.
         hits.push(text[j]);
         // We can remove the two lines below, because we want to log to the console later on, not in this loop.
         console.log(j)
         console.log(text[j])
      }
  }
};

// Here we log to the console.
console.log(hits);

#4

thanks so much! :blush:


#5

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