# Slight problem with Function Decalrations lesson?

#1

I know for a fact that the code I typed should work, but for some reason unknown the lesson is telling me it’s wrong. Look for yourself if you wish.

Anything I did wrong apart from not spreading it along multiple lines? Because I actually don’t see a single problem with that code.

Edit: I wish I had realized I typed “numbrwo” before I posted…

#2

There is a lot of value in whitespace when it comes to helping us spot errors.

``````function isGreaterThan(numberOne, numberTwo) {
if (numberOne > numberTwo) {
return true;
} else {
return false;
}
}
``````

Note that `if` statements do not end in a semi-colon. They are recognizable constructs so the interpreter knows where they end.

}

#3

Yeah, I realized just after I made the post that I typed “numberwo” in the math equation instead of “numberTwo”.

Thanks a bunch for the help though, it’s helpful to know that if statements don’t need a semicolon since the lesson on if else statements didn’t describe that.

Also, generally for shorter expressions, I’ve noted that quite a few developers will write them on a single line, at least on the code sharing platform I use that has a modified version of Lua meant for game developing.

#4

Yes, for short expressions a single line can be fine.

``````if (condition1) return true;
else if (condition2) return true;
else return false;
``````

The interpreter can handle the above non-standard script, even without curly braces. I say non-standard because we will not likely see this is a text book.

``````if (condition1) { return true } else if (condition2) { return true } else { return false }
``````

Also valid since the interpreter can handle it, but it is plainly difficult to read and would benefit a great deal from whitespace as mentioned earlier. Whitespace if free, and it makes our job much easier when we use it to separate lines and indent nesting.

For the record, statements that do not need semi-colons at the end are as follows:

``````if else if else

while

switch

function

for
``````

None of the above is assignable, except for `function`. When it is assigned it becomes a function expression and will need to end with a semi-colon.

``````function foo() { }            // declaration

var foo = function () { };    // expression

const bar = () => { };        // expression
``````

Call expressions need a semi-colon. This can generally be applied to any statement ending in `)`, such as,

``````console.log(something);

foo();
``````

and also includes,

``````do { } while (condition);
``````

I say ending with as it only applies to the last character of the statement, not such cases as,

``````for () { }
while () { }
switch () { }
``````

We would never insert a semicolon if the statement is written like,

``````for ( ... )
{

}
``````

since the interpreter ignores the whitespace and parses the entire statement before executing. If we wrote a semi-colon after the first line it would stop parsing at that point.

#5

Wow, thanks! That’s a huge help! I’m definitely bookmarking this!

Do you need a semicolon after an array too?

#6

Yes, since it defined in an assignment.

``````const arr = [];

const obj = {};
``````

Any statement with an `=` sign is an assignment.

#7

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