HELP syntax incorrect


#1


i cant seem to figure whats wrong


#2

its done people , i just took the condition after the else statement???


#3

Yes else doesn't take any condition. Also posting copyable code might be easier for others to test the code:


#4

Hey this is a different issue from your question, but I noticed an error in your syntax from the picture you posted. In the following if statement in your picture:

while(slaying) { 
    If( youHit=1) {
    ......

You are using a single "=" sign. The single equals sign is an assignment operator, meaning when this line is read, it is setting youHit equal to 1, rather than checking to see if it is already equal to 1. To fix this, you need to use "==" (two equal signs), which is a comparison operator. This will check the current value.

Consider the following:

var i = 10;
if(i = 2)
    console.log("Hello");
else
    console.log("Goodbye");

The output for this, as written, is "Hello". This is because it is assigning the value of "2" to i, rather than checking if i is already equal to 2. When the second equals sign is added, it reads:


var i = 10;
if(i == 2)
....
The output for this code is now "Goodbye", since it is comparing the value of i. Hope this makes sense!

#5

Thankss alot , you've helped


#6

Well spotted. Just to get the terms correct. That would be a logical error and not a syntax error and these are way worse because they are harder to find. A syntax error would mean that there is something wrong with your sentence e.g. translated to English it would mean that you idk swapped from subject predicate object to predicate object subject or used words that are not part of the language and stuff like this. There the interpreter helps you by rejecting anything that it is not able to understand. Although many beginners hate it for doing so it actually ensures that what you do is correct so it is rather a good thing. Think about it if you have a larger project that really matters you'd rather like to know from the beginning if your stuff will work or explode when starting up. Logic errors on the other hand mean that although your sentence may be perfectly valid English it doesn't make sense or is interpreted in a way you did not intended it.

in this case the the youHit = 1 is syntactically valid because the if() expects a value and this expression has a value. It's value is the value you assign, which is 1. Now javaScript is a bit sloppy here and excepts any value not just booleans and converts them to boolean according to this list:
http://www.sitepoint.com/javascript-truthy-falsy/
So by using youHit = 1 you essentially said 1 which is true so its if(true) which is perfectly valid but clearly not what you wanted to do.