Stuck on second loop push problem?


#1

text = "Blah blah blah blah blah blah Eric \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";

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

// Look for "E" in the text
for(var i = 0; i < text.length; i++) {
if (text[i] === "E") {
// 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.length === 0) {
console.log("Your name wasn't found!");
} else {
console.log(hits);
}
var text = "Blah blah blah zack blah zack blah \
blah blah zack blah";

var myName = "Zack";

var hits = [];

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

IT SAYS Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable.

Please help. I compared it to the example and it looked the same. I even copy pasted the example, it worked, and I still got the same error message.


#2

WHOOPS copy paste issue THIS IS THE CODE:

/*jshint multistr:true */

var text = "Blah blah blah zack blah zack blah \
blah blah zack blah";

var myName = "Zack";

var hits = [];

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


#3

I think you need to write "zack" with a capital letter in your var text but I'm not sure though


#4

This is correct. The value of myName is Zack so should match at least one time within the text in order to pass.


#5

Hi guys ; I ve just copy paste your answer .
here is my computer's reaction :
"Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable. "

???


#6

Somehow it let me pass after capitalizing my names, but still isn't pushing values to the array and the console read "[ ]" and I would like to know if it's because I messed up.


#8

A post was split to a new topic: Only prints the first letter of my name


#9