Only a part of my name is shown on the screen


#1

This is my code and it's showing it's correct but I don't see how as only a part of my name is shown in the result.

/*jshint multistr:true */

var text = "Hey Sulaiman! it's been a\ long time. It was nice seeing you,\ Sulaiman!";

var myName = "Sulaiman";
var hits = [];

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

}

}
if (hits.length === 0) {
console.log("Your name wasn't found!");
}
else {
console.log(hits);
}
The only part of myName is printed is "[ 'S', 'u', 'l', 'a', 'i' ]"


#2

for (var j = i; j < (myName.length + 1); j++){

So try doing this!

for(var j = i; j < (myName.length + i); j++ ){
                                    ^

The logic behind this is kind of complex so I understand why you're having trouble. But essentially "i" will iterate itself until it finds the string "J" which is the beginning of your "myName" variable. After that, "j" will then iterate until it gets to the end of your "myName" variables length.

I hope this helps!
:+1:


#3

Yay! It worked! Thank you.

Although it printed my name twice :confused:


#4

You have your name in the "text" variable twice is why. If you take out the second "Sulaiman" it'll only print your name once


#5

OH! Thank you!

I thought it was a requirement to mention the name couple times in the 'text'.


#6

Can you please explain this part? How's "J" beginning of "myName" variable? As "myName" variable is "Sulaiman", right?

Thank you,
Safura


#7

I do apologize, I meant "S" and got a little flipped around, you're right it was supposed to be "S" since that's the beginning letter in "myName"


#8

Oh, that's okay. I was just making sure I didn't miss anything. :slightly_smiling: