Introduction to 'While Loops'


#1


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


I'm getting the error: SyntaxError: Unexpected token

Not sure how to get around this or what I'm doing wrong.


var getToDaChoppa = function(){
    var arnold = true;
do {
console.log("Arnold is a good governor")  
arnold = false;
};
while( arnold === true) { console.log("The choppa! Now!")
};

getToDaChoppa()


#2

Hey man,

So there's a few things we need to touch on to help you out :smiley:

Firstly, Global and Local Scope - we need to assign var arnold to the global scope so within the function Javascript can read it.. I'd recommend reading up on Scope because way smarter people than I have done a good job at explaining it :slight_smile:

So our code should look like this rn:

var Var = true;

var Function = function(){
 do {
...}
while(Var === true) {
var Var = false;
...}
};

Function();

Second thing to notice dude is my syntax.. Make sure after any line you write in JS you end it with a ; or else you'll get syntax errors :crying_cat_face:

Read up and look at Do/While structure and try to apply this to yours

Notice how you're placed arnold= false within your Do part.. This should go in the While part because your code is going to crash your browser after fixing the syntax issues because it will repeatedly call the Do part :skull:

Hope I've helped.. I'm still pretty new to JS so my understanding isn't :100: but I'm sure someone can rectify my answer lol

Basically I'd start a fresh post reading this answer.. Think it'll be easier that way.. Good luck :dvd:


#3

Check the comment I left above.

@jimmynames I thought scope only mattered if you're using the same variable name inside and outside of a function. In this code, arnold is local within the function.


#4

Yeh I'm not 100% on my understanding of Scope yet but when you run this code in the browser it crashes when you make arnold local..

var arnold = true;

var getToDaChoppa = function(){
/* Move arnold here to make it local */
    do {
        console.log("Arnold is a good governor");  
    } 

    while(arnold === true) {
        var arnold = false;
}
};

getToDaChoppa();

If you can offer an explanation as to why I'd massively appreciated it :}


#5

GLOBAL SCOPE ARNOLD


#6

Amazing!

I'm eating cereal rn but will process this tomorrow morning!

Cheers @biff75 :sunglasses::muscle::v:


#7

Got it! Thanks :punch:


#8

You all are awesome. I'm trying to redo this now. Thanks for the help!


#9

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