How to remove "Uncaught Syntaxerror: Unexpected end of input" from Javascript

Asked

Viewed 1,186 times

-1

I am running a script on the Google console and get the error "Uncaught Syntaxerror: Unexpected end of input". I did not find the syntax error of this code, I searched in previous questions, here and here, but I couldn’t find anything that could tell me where the error of this code is. It follows the code I’m trying to run on the console:

 <script>
        var cidades = ["São Paulo", "Criciuma", "Curitiba", "Itajai", "Rio do Sul", "Erichim"];
        for (var i = 0; i < cidades.legth; i++){
            var str = cidades[i];
            if(str.lenght == 8){
                document.write(str);
                document.write("|");
            }
        }

    </script>

Would anyone know how to eliminate this error to run this example?

1 answer

2


You have syntax errors: The correct is: length, that is, the length of the array cidades and the length of each item in that same array.

var cidades = ["São Paulo", "Criciuma", "Curitiba", "Itajai", "Rio do Sul", "Erichim"];
        for (var i = 0; i < cidades.length; i++){
            var str = cidades[i];
            console.log(str);
            if(str.length == 8){
                document.write(str);
                document.write("|");
            }
        }

Demo:

inserir a descrição da imagem aqui

  • I fixed this error and circled, but it returns me "Undefined" instead of returning the names of the cities. Should not return the names this time?

  • 1

    It returns Undefined because, in fact, it is not a function, however, shows on your browser screen the cities Criciuma and Curitiba

Browser other questions tagged

You are not signed in. Login or sign up in order to post.