# Stuck I don't understand Exercise 6 check if name is in string

#1

Exercise 6 Stuck! Link goes to the exercise
What does this code do exactly? Is my analysis good?
if (text[i]===myName[0]){
for (var i=1; i<myName.length+i; i++)
{
// Does this part of the code say: if the FIRST letter of your name is found, push your entire name //to the array? Hence if name is Colt but Chris is used in the text variable it will push Colt because /it found the letter C and then name is not found?

``````/*jshint multistr:true */

/*jshint multistr:true */

var text = "Chris is cool";
var myName="Colt";
var hits=[];

for ( var i = 0; i<text.length; i++){}
if (text[i]===myName[0]){
for (var i=1; i<myName.length+i; i++)
{
// Does this part of the code say: if the FIRST letter of your name is found, push your entire name //to the array? Hence if name is Colt but Chris is used in the text variable it will push Colt because /it found the letter C and then name is not found?

hits.push(myName[i]);}}
if (hits!==myName){
console.log("Your name wasn't found!");
}
else if(hits===myName){    console.log(hits);
}
//My analysis: as long as i is smaller than the length of the text var, add one to "i".
// then if text[number i is at] matches the beginning of my name, give extra attention to the letter.
//to do this, run another for loop inside the if statement and that's when I get lost.
// I guess myName.length+i is because index starts at 0 hence the need to add one to the index?``````

#2

So what the code is suppose to say/ do is "if the first letter of your name is found, print that letter and letters up to the length of your name."

This is what my code looks like for a reference.

``````/*jshint multistr:true */

var text = "Hello, my name is Oscar. How are you doing today? Me myself Oscar is very well, yes Oscar is very well indeed"

myName = "Oscar"

var hits = [ ];

for(var i = 0; i < text.length; i++) {
if (text[i] == "O") {
// If we find it, add characters up to
// the length of my name to the array
for(var j = i; j < (myName.length + i); j++) {
hits.push(text[j]);
}
}
}

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

Let me know if this helped you or if you need further explanation.

#3

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