6/7 unexpected token }


#1

here is my code
var text = "Jacob likes to code.\
Jacob is a swimmer ";
var myName = "Jacob";
var hits = [];
for (var i = 0; i < text.length; i++){
if (text[i] === "J"){
for (var j = 1; j < j+ myName.length; j++)
}
}
if (hits === 0){
console.log("Your name wasn't found");
}
else {
console.log(hits);
};
It returns unexpected token } Help please


#2

This is the problematic code block, you are missing brackets and you are not registering any hits.


#3

I have brackets there


#4

for (var j = 1; j < j+ myName.length; j++) **{** // You are opening a new for loop, that requires new brackets
//body
**}**

Then you need to edit the conditions for the for loop.

var j = 1; j < j+ myName.length; j++

In the body of the for loop register the letters with the "push" function


#5

first or second for loop


#6

and what do I need to write


#7

The previous exercise 5/7 "Your second for loop" in the instruction side there is this sentence:

your second should be something like
for(var j = i; // rest of loop setup
Second, think hard about when your loop should stop. Check the Hint if you get stuck!

Taken from Javascript 6.Search Text for Your Name exercise 5/7

you got the rest of loop right, just the beginning of it is incorrect.

You do the second loop so you could get the letters out of the text without messing up the first loop.
When the code reaches the second loop it means it has found the first letter "E" then you need to push all the letters to the array "hits" You have to write one line in the second loops body, the loop is doing the rest.

for (var j = i; j < j+ myName.length; j++) {
//body
}

How to push something into array:

newArray = [];
newArray.push('hello');
newArray[0];   // equals 'hello'

Taken from Javascript 6.Search Text for Your Name exercise 5/7


#8

um well i ended up skipping 5/7 because it kept crashing now I need help with 5/7 sorry!


#9

here is my code im confussed with th whole .push thing so yea its pretty messed up

var text = "Jacob likes to code.\
Jacob is a swimmer ";
var myName = "Jacob";
var hits = [];
for (var i = 0; i < text.length; i++){
if (text[i] === "J"){
for (var j i; j < j + myName.length; j++){
var newArray = [];
newArray.push("Jacob")
newArray[0];
}
}
};


#10

I would really recommend you to look through the exercise 5 again, it should work because i just did it.
I'll try to explain

var newArray = []

is used to create a new array, you have done it here:

Now the push.

newArray.push('hello');

is purely an example you have to use your own array, it is called "hits" and inside the parenthesis you have to put the letter you receive, in the example there is a string.

In the second array you get the letters like this:

text[j]

And lastly:

newArray[0]

outputs you the first element of the newArray, if we take the example then it outputs 'hello'
What you need to do is you have to console.log the whole array

console.log(hits)

#11

what do i need to .push?


#12

hits.push(text[j]);

Do not proceed before you understand the code, copy paste is generally a bad idea, but do it if you understand the code. Do that exercise one more time and be sure you know what you are doing.