Help with the for loop excerise


#1


Exercise 5:Your second 'FOR' Loop


I'm unable to crack this code no matter what I do can someone please help me with understanding this exercise.It would be great if helped with a proper example.



#2

Please post your code so we can help you correct your error :slight_smile:


#3

Please have a look at this.

var text = "Hi My Name is Raj that's right Raj did I mention it's Raj it actually is Raj"

var myName = "Raj"

var hits = []

for (var i = 0; i < text.length; i++); {

  if (text[i] === "r") {

    for (var j = i; j < i + myName.length; j++);
    hits.push(j);
}
}

Thanks.


#4

The r should be capital on this line"R",

You are missing two important detail your text cal ,and brackets [ ],

hits.push(text[j])


#5

My apologies, But I couldn't understand what went wrong here ?


#6

No this is what you need to include you were missing text[j] :slight_smile:

hits.push(text[j])

this is what you wrote at first,


#7

so far I have changed the code as below but the error persists


var text = "Hi My Name is Raj that's right Raj \ did I mention it's Raj it actually is Raj"

var myName = "Raj"

var hits = []

for (var i = 0; i < text.length; i++); {

if (text[i] === "R") {

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.