Syntax Error: Unexpected token on "when to while and when to for"!


#1



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


It says syntax error: unexpected token
Any help appreciated!!!


var she = true
var she = function ();

while(she){
    console.log ("You used while        successfully!");
    she = false;
};



for (var i = 0; i < 5; i++){
    console.log (i);
};

she();
i();


#2

Why did you initialize 'she' as a Boolean and then try to change it into a function? You also didn't end all your statements correctly. There is at least one missing semi-colon.

You then try to execute she() and i() but I believe you don't need to especially for the i() since it is nothing more than an integer variable.


#3

@devace71301

First

You set she variable to true then change it to a function (+ the function syntax is not good).

What I suggest Remove this line #please don't complicate your life

Second

Remove this line, you don't need to call for a loop to happen.


#4

Thanks a lot! It works now! But I got one more question! Why I shouldn't convert it to a function here? When should I change something into a function then?


#5

Thank you! But I don't understand when should I change something into a function?


#6

Double declaration. you declared your first variable she equal to true then on the second line you declared a function and you named it as she.

But in some instance, you can do it like this:

var she = function (){
while(she){
    console.log ("You used while        successfully!");
    she = false;
    }
};
she();

#7

For the purpose of this exercise you don't need to create a function. You only need to create two types of loops (for and while). You have all of the code there just remove the part where you assign a function to 'she' and remove both function calls at the bottom.


#8

@devace71301

A function is a part of code that you create to repeat blocks of code without having to repeat yourself. You don't usually use it in loop (well, I don't think I've ever seen someone using it though).

If you want to use function, they are pretty good because first, you won't have to repeat yourself. Here's an example:

// defining a function called Standard Response
var myStandardResponse = function() {
console.log("Get lost jerk!");
};
// on the bus

console.log("Hey lady, can you spare a dime?");

myStandardResponse();

// at the office

console.log("Can you handle Joe's workload, in addition to your own, while he's in Tahiti for a month? You'll probably need to come in early and work till midnight, but we are confident you can handle it. Oh, and we can't pay you extra because of budgetary constraints...");

myStandardResponse();

// at the party

console.log("Hi, haven't I seen you somewhere before?");

myStandardResponse();

The output might look like:

Ok, I know that's kinda rude but it's just an example. Second, it can be used for debug purposes when you're isolating a piece of code just to see what's wrong with the code and third, you can simply update it quickly and save time because you won't have to go to every place you've written that bit of code to modify it, instead, it's all in a single block so it will be easy updating it!

Hope this helped! :wink:


#9

It helped a lot! Thank you so much!:relaxed:


#10

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