6/7 won't just print my name?


#1

The code passes to the next stage but it prints out every letter even those that aren't in my name individually and even some undefined print out. What am i doing wrong?

text = "Drew yo yo yo yo yo Drew";

var myName = "Drew";
var hits = [];

for (var i= 0; i if (text[i] === "D");{
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

a for loop has 3 pieces:

for (initialize; condition; increase){
  // if you want a if statement, it is here between the curly brackets
}

you can't have a if condition while declaring a for loop like you did here:

for (var i= 0; i    if (text[i] === "D");{

#3

Sorry that's not what is on my screen, i tried to just copy paste my code onto here and the format is messed up. it actually reads:
for (var i=0; i < text.length; i++) {
if (text[i]) ==="D";{
for ( var j = i; j < myName.length + i; j++){
hits.push(text[j]); }
}
}


#4

your if statement is a bit off, you have:

if (text[i]) ==="D";{

it should be:

if (text[i] ==="D")

a if statement ends when the matching closing bracket is encountered, which in your case is before the condition is checked, also, there is a semi-colon there which i can't explain


#5

Wow thanks so much for your help! It works perfectly now and it was all because of that pesky semi-colon. Is there anyway you can explain why the semi-colon there screwed the whole code up?


#6

Not a super-low level, my understanding of javascript isn't that good. It is not only the semi-colon, it was also your misplaced bracket ()). I think the easy way to explain the missing semi-colon is like a dot (.) in a sentence, if you place a dot to early in a sentence, it doesn't make sense, same with a semi-colon