function checkScope()
{
"use strict";
let i = "function scope";
if (true)
{
let i = "block scope";
console.log("Block scope i is: ", i);
}
console.log("Function scope i is: ", i);
return i;
}
When this:
function checkScope()
{
"use strict";
let i = 'function scope';
if (true)
{
let i = 'block scope';
console.log('Block scope i is: ', i);
}
console.log('Function scope i is: ', i);
return i;
}
is not correct.
As you can see, the system accepted an answer with double quotes and not with single quotes.
This was not explained anywhere throughout the course(unless I missed it) and as a result I believe the question needs to be clarified for future users.
Is the issue the single quotes, or is it the opening curly bracket on another line?
What you have right now says āif trueā¦ Then nothing. and as a separate block, do this stuff.ā
In your context, single quotes, double quotes and backticks are the same. But as we can have a single-line if statement without curly braces, thatās what the javascript engine parses your code as doing. And we can have a curly-brace wrapped code block at any point, simply to group code, and thatās what the javascript engine thinks you want.
Move the opening curly brace to be beside your if clause, and it should be the same as the original.
Is this correct? I tested the code with the opening curly on the next line using node and it seemed to work fine. I personally always put the opening curly on the same line as the if statement but I swear Iāve seen it done the other way all the time.
const code = `
function checkScope()
{
"use strict";
let i = 'function scope';
if (true)
{
let i = 'block scope';
console.log('Block scope i is: ', i);
}
console.log('Function scope i is: ', i);
return i;
}
`
console.log(code.match(/(i\s*=\s*).*\s*.*\s*.*\1('|")block\s*scope\2/g)); // null
const code = `
function checkScope()
{
"use strict";
let i = 'function scope';
if (true) {
let i = 'block scope';
console.log('Block scope i is: ', i);
}
console.log('Function scope i is: ', i);
return i;
}
`
console.log(code.match(/(i\s*=\s*).*\s*.*\s*.*\1('|")block\s*scope\2/g)); // [ 'i = \'function scope\';\nif (true) {\n let i = \'block scope\'' ]
If curly braces are sensitive, that should be one of the first things taught in the lessons. Did I miss it somewhere?
Because as far as I can tell they are logically the same thing.
Replacing quotes was the only thing I changed to make it work, and whats more, console outputs both reported correct results.
That indentation style is more common in other languages. I seem to remember the style (well some variant of it anyway) being enforced in the CS50 course, at least for C. I donāt think there are many if any, JS style guides that have that style of indentation as recommended. One of the reasons given is the ASI rules can conflict with it, like in the return example.
The indentation (or lack of) in the second code example makes it harder to read in my opinion. But that is often how it goes with an unfamiliar style, it messes with your pattern recognition.
Personally, I wouldnāt mind if the JS challenges enforced a style guide. Teaching good code formatting is just as important as syntax and logic.
Reread @lasjorg last post again, placement of curly braces does not matter, this is an issue with the test.
@bbsmooth I read it like 10 times because I was sure quotes were the issue.
The link he posted clarifies that javascript has its own indentation style, of which, misuse can cause code to break.
From the post
For example:
return {
name: āJaneā
};
If you formatted that in Allman style, youād have:
// Donāt do this
return
{
name: āJaneā
};
However, JavaScriptās automatic semicolon insertion [2] adds a semicolon after return, if it is followed by a newline. The above code is thus equivalent to
return;
{
name: āJaneā
};
@triangle4studios If you are ever in doubt press Shift + Alt + F in the code editor. It should give you standard JS formatting.
Itās probably safer to stick with that style as a lot of challenges use regex to test the code and sometimes all it takes to break the tests are unexpected formatting. It shouldnāt break with valid syntax, but it can be harder than it sounds to account for all coding styles when writing the regex.
Javascript needs to address this. It is very inconsistent. I dispise non-allman style formatting. I truely believe brackets should be in line with their opposites.
It aids in readability.
It is up to each person or team to enforce whatever style they prefer. But I can almost guarantee that you are not going to change the general consensus about whatever the currently most accepted style is, in whatever language.
There is really no way you can say there is a right or wrong style. If it was that simple there wouldnāt be so many different styles, to begin with. Even if some people have gone so far as to name some styles āthe one true brace styleā (K&R + variants) that is just hyperbole. There are also historical reasons, and language specific syntax reasons, why each language has its own set of style guides that do not agree with each other.
Personally, I find brace style wars utterly uninteresting, like most other coding wars. The only thing I get evangelical about is inconsistency. I donāt ādespiseā whatever style a project uses, so long as they are consistent and clear.
I donāt think I have had anyone go so far as to call another personās whitespace and brace preferences āunprofessionalā. The only thing thatās unprofessional is refusing to use the style guidelines for your current project. Each project gets to decide what āprofessionalā style means in their codebase.
All of the C-style languages, as far as I know, can use any of the main brace styles. You can write C# with Allman, K&R, 1TBS, Horstmann, GNU, or whatever you like. It really doesnāt matter. Same goes for C, C++, JavaScript, etc. There is nothing to āfixā. Whitespace does not have syntactic meaning in C-style languages on purpose. If you want whitespace to have meaning, look at other languages, like Python.
I personally prefer 1TBS. It seems to me be readable and not generate superfluous lines only for opening braces, and it seems to be the most common style Iāve run across. Iāve also worked in codebases that use Allman without complaint. Itās not my favorite, but it is fine. I just use the formatter to make sure that my code meets the standards of the project Iām working on.
Iāve always followed Kernigan, but i imagine itās what one picks up along the way. Most of my languages of choice have always been interpreted (or JIT-compiled, like javascript), and nearly always used the curly brace on the same line as itās triggering clause.
I wonder if the preference thing follows compiled vs interpretedā¦ But i suspect that most dev shops, is they donāt have one, soon develop or adopt a style guide.
Thatās not what he said at all. You can use Allman just fine in JavaScript.
The freeCodeCamp test suite uses Regex to verify some solutions. Those Regex sometimes assume 1TBS since it is the most common style in JS.
In this challenge, that is a fair assumption, as it is very uncommon for users to completely rewrite portions of code unrelated to the task they have been given.
It isnāt that nobody is acknowledging some secret flaw youāve discovered in JS. Itās that the flaw that youāre seeing does not exist.
Thereās one specific place where adding a newline matters (directly after the keyword return), thatās it, and itās extremely well known. The situation where it occurs in error isnāt common anyway, and it doesnāt have any affect on Allman style, which specifies putting the opening bracket of a block on a new line; the issue doesnāt occur with Allman style [edit: any more than with 1TBS] (you canāt return a block in JS, so the keyword will never appear before one in otherwise-valid code)