Still not pushing values to hits array. What's wrong with it?


#1

/*jshint multistr:true */
var text = "oasou uasu U SGCH hga James augsckg whwej av James ugkjc kugef Jamie kuvtbe Ja,es James ulayvj, Jamie Jamesiue khgsc James oiyjebw";

var myName = "James";

var hits = [];

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

console.log(hits);

I've tried this out again and again and I have no idea what's wrong with my code. It looks similar to other people's example but mine is still not working. Why's that??


#2

Currently getting this message:

"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."


#3

Problem is related to both for loops:

for (i = 0; i > text.length; i++) {
for (j = i; j > i + myName.length; j++) {

They will not be executed, not even once.

The first for loop:

i = 0
text.length = 130

And you want to execute block of for as long as i is greater than text.length. Problem is that 0 is not greater than 130.

You have to reverse conditions of both for loops.


#4

Whoooops!! Oh man. How did I miss that one.

Thanks so much. :slight_smile:


#5

You're very welcome :slight_smile:


#6

shuold be for(i= 0; i < text.length; i++)... same as the other one with j


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.