Your second for loop


#1

I dont get why this doesn't work

/*jshint multistr:true */

var text = "Hello, i am Nick. Do you know a lot of people\ called Nick?";
var myName = "Nick";

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

The error: 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.


#2

@coding_is_awesome in your second for loop, you are telling it to loop as long as j is equal to (i+myName.length). that is false the first time around, so the loop quits. change the === to < or <= and it will work.


#3

I tried that it didn't work


#4

I think this is broken, I've been working on this for the last hour when I finally gave up and pasted from the original example and still get an error :frowning:


#5

without seeing your code, i can't tell what you may be doing wrong.


#6

I entered the below and get
"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."

When I head over to code academy labs I get no errors. I then cope the example from "what you'll be building" and paste it (minus the last if else) and I still get the same error.

var text = "The quick brown fox jumps over the lazy dog"
var myName = "Jim";
var hits = [];

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

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


#7
var text = "The quick brown fox jumps over the lazy dog"
var myName = "Jim";  <== Try to find this in 'var text'
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]);
        }
    }
}

basically you're trying to find 'J' somewhere in "The quick brown fox jumps over the lazy dog"
but its not finding it anywhere. add a 'Jim' somewhere in that text


#8

no dice. I get no different in either replacing Name[0] with "Jim" or "J" and don't get anywhere by adding Jim or multiple Js to the text

var text = "The quick brown fox jumps over the Jim lazy dog J J J "
var myName = "Jim";
var hits = [];

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


#9

remove the semicolons after your For loop's test condition brackets ( and ), otherwise you're telling Javascript to end the statement there.

if (hour < 18) { greeting = "Good day"; }

#10

Just as a point of reference, heres' the code I used and it works.

/*jshint multistr:true */
var text =" dsafds sdfdf lkj Serge asdfsjf serge asdfsadf serge ";
var myName="Serge";
var hits = [];
for (var i=0; i< text.length; i++){
if (text[i]==='S'){
for (var j=i; j < (i + myName.length); j++){
hits.push('text[j]');
}
}

}


#11

Hi, i have been looking for this answer for a while, ive tried alot of possible solutions. nothing works, I'm beginning to believe this challenge is broken. could someone confirm or deny for me. Thank you


#12

text="webjfviqpobwevqwebpoqeviueryv \
eqrvqerthomasbgvgrhgvgyueqvhcevvcevrwbveqg \
ewbvcqegfvyucegvguegvyuqebvwvucybqvbvyuqvq \
wvjbhqwebviuqwbvbthomashqwiuvbquwbvcyuqwberuv \
hbbcviqbvqwgvvyubqwvbihybwhebvihbwivd \
wjebthomascvhqebgvviuaebgvqewbvebihrvebihbvrr"
myName="thomas"
hits= [];
for (var i=0; i if (text[i]==='t'){
for (var j=i;j<=myName.length; i+6)
hits.push(myName)
}

i don't know what to do now?


#13

Oh man thank you that did it!