Whats wrong with this code . It aint working


#1

Whats wrong with this code. and also if you can explain a bit more on hits push method

/*jshint multistr:true */
var text = "Hey, how are you \
doing? My name is Manish.";
var myName = "Manish";
var hits = []
for (var i=0; i <= text.length; i++) {
    if(text[i] === myName[0]) {
        for(j = i; j < (myName.length+1); j++) {
            hits.push(text[i]);
        }
        }
    };

#2

Hello @manishbjain,

In the body of your second for loop, you must .push() to the hits array, the jth element of the text string, not the ith element. Fix this up and you'll be good to go!


#3

I tried ..but it aint still working. see the code below

var text = "Hey, how are you \
doing? My name is Manish.";
var myName = "Manish";
var hits = []
for (var i=0; i <= text.length; i++) {
if(text[i] === "M") {
for(j = i; j < (myName.length+1); j++) {
hits.push(text[j]);
}
}
};


#4

Put j < (myName.length + i) not 1

If it still doesn't, try removing the semicolon after the last curly brace...


#5

ok it works but can u please explain me as to what this code actually does...

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


#6

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