5.Practice makes perfect Illegal Error? Help (Solved)


#1



https://www.codecademy.com/courses/javascript-beginner-en-ASGIv/1/1?curriculum_id=506324b3a7dffd00020bf661


It always says SyntaxError: Unexpected token ILLEGAL, any hints or tips?


//Remember to set your condition outside the loop!
var count = 0;

var loop = function(){
while(count < 3){
console.log("I'm looping");
count++;
}
};

loop();


#2

your string isn't correct. missing something at the end


#3

I really can not see anything


#4

here is a hint!!!!!!!


#5

Still won't work :frowning:

SyntaxError: Unexpected token ILLEGAL

var count = 0;

var loop = function(){
while(count < 3){
console.log("I'm looping!");
count++;
}
};

loop();


#7

worked for me. try to refresh


#8

Wont work for some reason...


#9

I am getting the same error.


#10

I've gone through this again.

//Remember to set your condition outside the loop!
var count=0;
var loop = function(){
while(count<3){
console.log("I'm looping!");
count++;
}
};
loop(3);


#11

Make sure that L isn't supposed to be capitalized


#12

Hello there, I just had a question. Why does the function( ) not have a variable inside of it like function(count)?
This code worked for me, but not sure why the function has open parentheses

var count = 0
var loop = function(){
while(count<3) {
console.log("I'm looping!"); count++;
}
};

loop(2);


#13

Well the function syntax is just:

    var name = function(){
        code
    }

and to call it:

    name();

The parameters (that is the term for those thing like count inside the brackets) are optional. What they do is to declare a local variable inside the function that has an interface to the scope outside of the function. In an example:

    var loop = function(count){
        while(count<3) { 
    	console.log("I'm looping!");
            count++;
        }
    }

    loop(2);

This would do pretty much the same as:

    var loop = function(){
        var count = 2; //this line instead of the parameter
        while(count<3) { 
            console.log("I'm looping!");
                count++;
            }
        }
    }
    loop();

The big advantage of the first version is that you could use anything as argument (the thing you put in the () of your function call) and are not limited to a fixed value of e.g. 2.

So what is happening is that the value in the () of the function call: loop(2);
is assigned to the parameter used in the function definition: var loop = function(count){
So if you want to loop 3 times instead of once you just need to use loop(0) instead of loop(2) in the function call and do not need to modify the function itself.

So after the theory back to your example: What you're doing is using an argument in the function call of a function that has no parameter. This is more information than this function needs or has asked for so it is just going to ignore it but instead uses the count your declared in front of the function.

Hope this helps you understand it a bit better.


#14

Thank you so much! This was extremely helpful!! I have one more question if you could possibly answer.

In your example you declared "var count =2" as a local variable inside the function as opposed to my "var count =0" as a global variable outside the function. How come? Does it make a difference?

Thank you again.


#15

I just wanted to illustrate what a parameter does:

var loop = function(count){

is basically saying:

    var loop = function(){
    var count = /*magicExternalInput;*/

So your question is good because if you'd used a parameter count in your function that would have made the external global variable count inaccessible from inside the function (because there is a variable as well that already occupies this name). As said a parameter is in the end just a regular local variable. Was it clear enough or too confusing?


#16

So...does anyone know how to fix this code thing? My code still won't work(Posted above)


#17

Could you post the link to the exercise? Because the code alone should be working.


#18

https://www.codecademy.com/courses/javascript-beginner-en-ASGIv/1/1?curriculum_id=506324b3a7dffd00020bf661

So is there any way for me to just pass the exercise now, because everyone is saying the code sould work.

Error is "SyntaxError: Invalid or unexpected token"- now


#19

https://www.codecademy.com/courses/javascript-beginner-en-ASGIv/1/1?curriculum_id=506324b3a7dffd00020bf661


#20

Well using your code (copy&paste) I'm able to pass the exercise you linked to. Meaning I cannot reproduce your error :frowning: Maybe just refresh the page and try again.


#21

I've tried plenty of times