Unexpected end of input error on Search Text for your name


#1



SyntaxError: Unexpected end of input, Oops, try again. There was a problem with your syntax.


/*jshint multistr:true */

var text = "text text text dog text text text";
var myName = "dog";
var hits = [];

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

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


#2

here:

for( var i = 0; i <text.length; i++) 
{

the { of your first for loop has no matching closing curly bracket (})


#3

when i add more closing curly brackets it freezes the browser


#4

yea, because your second for loop is an infinity loop. Maybe revisit 5. your second for loop to fix the infinity loop?


#5

i changed the loop to

for(var j = i; i+ myName.length; i++)

a new error message shows: SyntaxError: Unexpected token ]

it seems to be another syntax error, i cant find any extra misplaced brackets though


#6

And why did you decide to change your loop this way? Why is this loop now no longer an infinity loop? Does the loop for-fill its purpose?

The syntax error could be anywhere, i would need to see your full code


#7

/*jshint multistr:true */

var text = "text text text dog text text text";
var myName = "dog";
var hits = [];

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

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

#8

i found the additional ], it is in this line:

if(text[i] === 0])

your loop is still an infinity loop, i can help you further but please start answering my questions, else i am going to ask someone else to help you further


#9

thank you that solved the token error now it is telling me SyntaxError: Unexpected end of input


#10

we are back to where we started


#11

ok an infiinty loop im thinking the loop would have the variable i =0 which would be the 'd' in dog. when the for loop detects the d in dog im guessing the loop should count the two more letters to arrive at the word of dog 0, 1 ,2, that is why initially i had the myName.length = 3 because my real name has four letters, forgive me if i sound stupid im relatively new to this


#12

i dont know why im typing the stuff yet i only have a rudimentary understanding. i=0 so when using the name dog there are 3 characters i want the for loop to count when the first letter matches the d in dog and return to me the numerical value of how many letters is in dog starting with 0, im learning that i dont have the full understanding of the how to for loop functions so detecting my own errors is difficult


#13

the infinity loop is in the second loop. When will the second for loop ever evaluate to false?


#14

for(var j = i; (i+ myName.length = 2); i++)

would it evaluate to false when any number of letters isnt equal to 2, for example the word text has 4 letters so that word would be excluded?


#15

the purpose of the second for loop is to find your name in a string, this string you stored in a variable named text.

i will contain the index of the first letter of your name. i + myName.length will contain the index of the letter after your name. This is handy, because it means we can do:

j < i + myName.length;

then we increase j (not i) to loop over your name. since now we have a start point of your name (i) and a stop point (i + myName.length) for "dog" in text


#16

so i is not just a numerical value of 0 it is the begining point of the loop


#17

i is a numeric value, but 0 it is indeed the start value of the loop. look:

var text = "text text text dog text text text";
for( var i = 0; i <text.length; i++) {
    console.log(i);
}

we increase i with the for loop (i++ is a shorthand for i = i + 1).

We use the first for loop to loop over the string stored in text. i is a number, which we can use as index to get the letters from the string stored in text:

var text = "text text text dog text text text";
for( var i = 0; i <text.length; i++) {
    console.log(text[i]);
}

So the value in i changes over time, that is why we have an if condition:

if(text[i] === myName[0])

to check if the current letter in text equals the first letter of your name, if so, we start pushing our name found text to the hits array (using a second for loop)

That is the point of this program


#18

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.