I can't get my head around step 5 and 6


#1

Hi to everyone,

I am really stuck in this exercise, although i passed the whole chapter
But it never puts out the right "hits" like in the example, please help!!! what's wrong with this code?

var text= "Blah blah blah blah blah blah Anne \
blah blah blah Anne blah blah Anne blah blah \
blah blah blah blah blah Anne";
var myName="Anne";
var hits = [];

for(var i = 0; i < text.length; i++) {
if (text[i] === "A") {
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);
};

console says: [ ' ', ' ', ' ', undefined ] well done, start next lesson gnarf

Thanks to everyone


#2

hi found out myself

/*jshint multistr:true */

var text= "Lorem ipsum dolor Anne sit amet, consectetur adipisici elit, sed\ eiusmod tempor incidunt ut labore et Anne dolore magna aliqua. Ut enim ad minim\ veniam, quis nostrud exercitation ullamco laboris Anne nisi ut aliquid ex ea\ commodi consequat. Quis aute iure Anne reprehenderit in voluptate velit esse\ cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non\ proident, sunt in culpaAnne qui officia deserunt mollit anim id est laborum.";
var myName = "Anne";
var hits = [];
for (var i=0; i < text.length; i++) {
if(text[i] === 'A'){
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);
};

deleted the semicolon in line 8 before the curly brackets and it worked

:muscle:


#3

Hi terasolver...Im glad you found the answer. Just wanted to note that you don't need any semi-colons after any of your brackets.


#4

Hi bitjumper...,
just one question: Why don't I need the semi-colons?
I thought i have to close every function, var etc. with a semi-colon.

greetings Anne


#5

So you need a semi-colon after declaring a variable:
var i = 0;
or after a statement:
document.getElementById("example").innerHTML = "Hello Arab";

It is telling the computer this statement needs to be executed or value stored.

A Semicolon after a function is not necessary, but they are optional. Meaning they wont affect the code and cause an error.