What am I doing wrong?


#1

It cant find my name and will not give me back anything in the array. Something wrong with the second "for" statement or the push method?

/*jshint multistr:true */
var text = "Once upon a time there was a school called fusion academy. Graham and Austin were bored so Austin decided to watch Graham code some javascript. Who knows what else they could do. Graham is an OG, and so is Austin.";
var myName = "Graham";
var hits = [];

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

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


#2

Its wrong , ill send my example and you see what's your error.

/*jshint multistr:true */
var text = "Blah Blah Blah Blah Ricardo Blah \
Blah Blah Blah Blah Ricardo Blah Blah Blah Blah \
Blah Ricardo Blah Blah Blah Blah Blah Blah Blah \
Ricardo Blah Blah Blah Blah.";

var myName = "Ricardo";

var hits = [];

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

if (hits.length === 0) {
console.log("O teu nome não foi encontrado!");
}
else {
console.log(hits);
}