Getting Syntax errors on review 11


#1


I'm stuck on Review 11, introduction to why loops in JS.


var caligula=function(){
    var smart
        do{smart=false;console.log('Caligula was not');}
        while(smart=false;){console.log('very');smart=true;}
        for(i=0;i<1;i++){console.log('smart');}
        }

Can anyone tell me what I'm doing wrong?


#2

We can benefit by using lots of white space in our code. It is free and does not affect the code. It does boost readability significantly.

var caligula = function () {
    var smart;
    do {
        smart = false;
        console.log('Caligula was not');
    } while (smart);

    smart = true
    while (smart) {
        smart = false;
        console.log('very');
    }

    for (var i = 0; i < 1; i++) {
        console.log('smart');
    }
}

Note that I added a while () condtion to the do {} while ().


#3

Bro, +1 save smart = true, you forgot the semi colon before the while loop. You also need to initialise var smart at the top of the function with var smart = true; so js knows its a boolean. Also, you wrapped the loops in a function called caligula but did not call it after its implementation with caligula();


#4

JavaScript variables do not have a type. Only values have type. In the above, smart is declared. It is defined inside the loop body.

The missing semi-colon is minor and won't raise an error since it's on its own line.

I didn't wrap the code in a function, the OP did. One would assume he knows to call the function to see it work. Not my problem.


#5

You are not correct, Javascript has data types, variables are 'dynamically typed' which in this case will technically work because var smart is dynamically typed as undefined and as its a Do While loop, it will iterate once where it is retyped Boolean. If it was a while loop for example. this would not work. For new coders, this is also very bad practice as they introduce error possibilities by typing variables undefined. FYI, all high level computer languages have data type, we only equate variables and data types to "Strongly Typed" where we explicitly define the data type of a variable or "Dynamically Typed" where we may leave the data typing to the intrepretor. There are use cases in all dynamically typed languages where typing needs to be explicit. For more on Javascript data types, see a great resource: http://www.w3schools.com/js/js_datatypes.asp


#6

Which means they take their type from the value/expression assigned to them. JavaScript gives primitives wrapper objects of appropriate class. "" gets a String wrapper, 0 gets a Number wrapper, true gets a Boolean wrapper, null gets an Object wrapper.

undefined is not a type, but a built-in response. The declared variable is not dynamically typed until it gets a definition. Until then, it is just a pointer to a reserved location in memory.


#7

I know what they mean dude but I think you need to follow up on your understanding of them! So if you don't specify a data type by instantiating your declaration, the absence of a true or 0 or string means the interpreter must guess which it cannot! It data types the absence of a datatype as undefined (i.e. data type is unknown) and returns this typing to the variable. When you assign a value, then it assigns a corresponding data type such as var smart = true; only then does the interpreter know what to do with the variable, which in this case is to treat it as a variable. I encourage you to review the link I posted on js datatypes, its good reading!

FYI, all dynamic data typing (incl undefined) is done by the compiler thus is a "built in response" returning a data type to the variable. With Java lets say, its strongly typed so you tell the compiler you want this variable to be a Boolean e.g Boolean smart = true; which sees the compiler to look at only Boolean type returning it to the variable. As you say, dynamic typing is done by the Interpreter who types based on declaration + initialisation value e.g var smart = true. This saves you having to be too worried about what data type is most suitable in most cases. That said, data typing in dynamic languages is common enough for use cases as you progress on it. I have better Python then JS and have done it there a good few times.


#8

Where is the guessing in the above? A do-while loop doesn't need a defined variable, only a declared one so it doesn't have to use var in the loop.

There is nothing wrong with declaring all variables at the start of the program. They don't need values, just the declaration so they get hoisted into their respective scope. Values don't get hoisted, anyway. We only need to assign values (define) before we refer to them. In the do loop above, the definition takes place before the conditional expression. No harm, no foul.

We spoon feed beginners so much, they hardly have to think for themselves. I'm am not of the mind to tell a learner they must do something, when in truth they don't have to IF they understand variables.


#9

lol... When you declare a variable, you must define it. In js, the intrepreter then looks for a datatype and returns it based on the value assigned in the initialisation. If there is none, then it cannot guess its a Boolean, so it data types as undefined! This undefined data typing completes the definition. Correct on the Do While, the code inside the it can assign a value in the first iteration but to someone that is not aware of the limitations and/even know of it at all, it introduces error possibilities due to not knowing datatypes. We can agree to disagree on "spoon feeding beginners". We both started somewhere and given some of the poor answers I saw on this forum, folks need ALL the help they can get! Most wont have any computer science training making any correct help necessary and any fully or partly incorrect help a real hindrance to understanding and progressing into the field.


#10

It's a long way from a free beginner site to 'the field', with a lengthy segue through a formal learning environment where copying and pasting is not an option.

Still don't agree with must. That's like saying we must use return in a function. It makes sense that a variable will be assigned a value before it is referred. Otherwise it matters not.

Must do thinking is what leads to this,

if (a > b);
{

};

We see this all the time, and it is because somewhere, somebody read,

"We must end all lines with a semi-colon."

It only leads to black and white thinking and focuses on syntax rather than concept. I like to press the concepts, and let the syntax fall in.


#11

FYI, there are allot of people out there who don't get formal education in computer science, informally learning enough online and at meetups to become decent programmers. You can disagree with me all you want dude, it does not make me any less correct! Instead of arguing like a computer language is going to change its mind about how It does data typing of variables, I suggest you use that time to look it up and understand how of it works! I am leaving this with you as a very final thought.. Nothing more to positively contribute!!!!


#12

And if you know this, then why troll this site?

JavaScript can change it's mind on any variable, any time. Just give it a new value of a different type. That's what dynamic typing is about. The variable is not bound to any class. Only what it refers to is typecast. The value is bound to a class. JavaScript handily wraps it in an object wrapper of that class, if it happens to be a primitive.

Then again, it's not JavaScript changing its mind, but the programmer.

The reason I harp on 'declare' and 'define' as two separate operations is because they raise two separate exceptions. ReferenceError when undeclared, and TypeError when declared and not defined. Only the first is a deal breaker. The program will not run. But with a declared, undefined variable the program will run (until it meets this exception during run time). It's stuff like this that learners at this level need to understand. Not so much 'field' related duties.


#13

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