ES6 - Compare Scopes of the var and let Keywords

Tell us what’s happening:

Can someone please explain to me why the following code does not work for the “compare scopes for var and let keywords:

function checkScope()
{
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;
}

checkScope();

Your code so far

function checkScope()
{
  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;
}

checkScope();

Your browser information:

User Agent is: Mozilla/5.0 (iPhone; CPU iPhone OS 17_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3.1 Mobile/15E148 Safari/604.1

Challenge Information:

ES6 - Compare Scopes of the var and let Keywords

It looks like it is failing because you changed the curly braces here

and here

I would suggest resetting the lesson and only updating the var keywords to let keywords.
make sure not to change anything else

then the test should pass

Hey @jwilkins.oboe , I just checked and it exactly the curly braces problem. I also checked in the other IDE, the new line curly braces work well.

As I understand, the newline curly braces are fine in JS. So is why it’s so? Is it a bug?

It looks like the tests didn’t account for changes in spacing and curly brace placement.
So that is why it failed in this testing environment.

Technically, that is true.
But most JavaScript developers and codebases will have the opening curly brace on the same line like this

function checkScope() {

in general, it is always best to follow common practices in that particular programming community.

plus most formatters like prettier will automatically format it in that way too.

hope that helps

1 Like

Thank you it worked, I am also taking the CS50x course and that’s is how they ask us to write our code.

For example:

If (condition)
{
// do something
}

My mistake.

1 Like