Question on 5/7 and 6/7


#1

var text = "Hello, my name is Jason."

var myName = "Jason"

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);
}

I successfully completed the exercise using the above code after searching the forums for ideas, but originally I was using hits.push(myName[j]) instead of hits.push(text[j]) but it generated an array with 5 elements each of which were "undefined" so it looked like this ["undefined", "undefined", "undefined", "undefined", "undefined"] Why doesn't this work? Isn't the variable myName equivalent to text? Is it because the initial condition on the if condition is based on text[i]?


#2

Consider what value j has

and, this is something you can answer yourself:

And the answer to that goes a long way to answering your main question.


#3

Here is my exact code that worked.

===Begins after this===

/*jshint multistr:true */

var text = "Hey, how are you doing? My name is Donovan.";

var myName = "Donovan";

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);
}