Log it!


#1

I'm getting the "Way to go!" message, but it is only printing out [ 'B' ] in the results window. Shouldn't it print out [ 'B', 'r', 'i', 'a', 'n' ] ? I've tried over and over and can't get a better result.

var text = "Humpty Dumpty sat on a wall. Humpty Dumpty had a great \ fall. Humpty and Brian were not friends at all.";
var myName = "Brian";
var hits = [];
for (var i = 0; i < text.length; i++) {
    if (text[i] === "B") {
        for (var j = i; j > (myName.length + i); j++);
        hits.push(text[j]);
    }
}
if (hits.length === 0) {
    console.log("Your name wasn't found!");
} else {
    console.log(hits);
}


#2

Should be j < ....


#4

I'm having the same problem as OP, but my code already has the <

/*jshint multistr:true */
var text = "Luke has a Luke with the Luke things. He does the things and nobody really understands why he does the things. It looks like Luke knows that text wraps without using the slash so he won't use it. So Luke."
var myName = "Luke";
var hits = [];

for (var i = 0; i< text.length; i++){
if (text[i] === "L"){
for (var n = i; n < (myName.length + i); n++){
hits.push(text[i]);
}
}
}

if (hits.length === 0){
console.log("Your name wasn't found!");}
else {
console.log(hits);
}


#5

That should be text[n].


#6

Works...and as I was phrasing my question on why, I figured it out. The variable i isn't changing when I increment n to check the next letter as part of my name, so when I push i while in the nested loop, it just re-prints the letter at that position.

Thanks!


#9