Lesson 5 : Your second "for" loop


#1
/*jshint multistr:true */

var text = "Sparky is really cool \
I love Sparky very much.  Sparky is the \
best.  Sparky is awesome";
var myName = "Sparky";
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])
    }
};

I have no idea what I am doing wrong can you please help?


#2

One word: "BRACKET!!!" You are missing a ending bracket! the "if" statement has an ending bracket, and so does the "for" statement, but what about the "for statement that's nested inside the "if" statement? It doesn't have an ending bracket. Please give it one.


#3

thanks krazykodekat. It worked!


#4

I am on the next lesson and it is saying I can be done but the wrong code is coming out.

/*jshint multistr:true */

var text = "Sparky is really cool \
I love Sparky very much.  Sparky is the \
best.  Sparky is awesome";
var myName = "Sparky";
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])
        }
    }
};
if("/*hits.length is 0*/"){
    console.log("Your name wasn't found!");
}
else{
    console.log(hits);
};

It is supposed to use the else statement in the last couple lines, but instead it is using the if statement. Can you please help?


#5
/*jshint multistr:true */

var text = "Sparky is really cool \
I love Sparky very much.  Sparky is the \
best.  Sparky is awesome";
var myName = "Sparky";
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])
        }
    }
};
if(hits = 0){
    console.log("Your name wasn't found!")
}else{
    console.log(hits)
};

I even tried reformatting it, and the else statement came out, but it came out as zero


#6

it is supposed to come out

["S","p","a","r","k","y"]


#8

    var text = "Sparky is really cool \
    I love Sparky very much.  Sparky is the \
    best.  Sparky is awesome";
    var myName = "Sparky";
    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])
                }
            }
        };
        if(hits.length === 0){
            console.log("Your name wasn't found!")
        }
        else {
            console.log(hits)
        };

You are missing hits.length in your if statement.