Text elements not pushing into hits array, Why?


#1

/*jshint multistr:true */

var text = "Hey, Noah how are you doing? I want my kids to clean the house all ready. Right Noah";
var myName = "Noah";
var hits = [];

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

console.log(hits)

Why isn't this pushing the values into the array and what would the correct code be?

Thanks


#2

The code should be like this...

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


#3

var myName = Someones Name; // 'Someones Name' is 13 characters, that's counting the space also.
for(var j = i; j < (myName.length + i); j++) { // 'someones name' starts with (s)
hits.push(Someones Name [s]); // Make sure to define 'Someones Name!
// This code will work, after some debugging