2nd for loop - Can't print to array


#1

Hello Guys, please assist.

I've been trough the forum here, trough MDN, Google, W3 and i can't find the error here... could anyone please assist ?

/*jshint multistr:true */

var text = "This dude can't realy get the for lopps, \
this person José is nabcake";
var myName = "José";
var hits = [];

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


#2

the problem is in your first for loop, that is false from the beginning, so it never runs


#3

There was an error i just spoted (since i made many tries)

where you reed > if (text === myName[0]){ , it should be if (text[0] === myName[0]){

already corrected and nothing yet


#4

we use our first for loop to loop over text (your condition for this is wrong), then we need to check if the current character in text (text[i]) equals the first letter of myName


#5

It's corrected, you where right it was the 1st if statment...
the correct way was

if (text[i] === myName[0]){


#6

now you just need to fix your first for loop, for the first run of your loop, the condition is false, so the loop never runs, so the if statement and second loop are never reached


#7

It's runing now. i've made a change in the 1st for loop regarding the 2nd statement

var text = "This dude can't realy get the for lopps, \
this person José is nabcake";
var myName = "José";
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] );
        }
    }
}

#8

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