Possible update?

Tell us what’s happening:
Describe your issue in detail here.
This might be simple, but the challenge is considered done when I change the if condition into false. Is it supposed to be that way?
Your code so far


function checkScope() {
"use strict";
let i = 'function scope';
if (false) {
  i = 'block scope';
  console.log('Block scope i is: ', i);
}
console.log('Function scope i is: ', i);
return i;
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36.

Challenge: Compare Scopes of the var and let Keywords

Link to the challenge:

Yeah, that is not what is intended. I think adding the false to the if just makes it unreachable code so it doesn’t get parsed somehow in the test. It seems like the test could be written better.

Are you asking because you are confused about what this lesson is teaching and don’t know how to solve it the “right” way? Or are you trying to point out a problem with the site?

Hi, thank you for the reply. I was trying to point out a possible problem with the question 's solution (which is declaring a false in if statement and thus might give a false impression of right answer), although it is very minor. I passed this part but I always check for different ways of solving the questions here in freecodecamp for ideas.

I initially got confused since the right answer is declaring the i inside the if statement using let, and I thought let couldnt be used to declare different variables with the same name. I just realized the difference is global and block coverage (I think?).

OK, cool, I’ll move it over to the Contributors section where we discuss such things. We’ll probably make a git hub issue to get it address - or you’re welcome to create it if you want.

Good catch.

1 Like

The second or third test will always pass, depending on the location of the ‘function scope’ variable. I guess one option might be to turn the if statement into an empty code block, just for the scope.

function checkScope() {
  var i = 'function scope';
  
  {
    i = 'block scope';
    console.log('Block scope i is: ', i);
  }
  
  console.log('Function scope i is: ', i);
  return i;
}

Although that would need an explanation. Which if the challenge text wasn’t so long already wouldn’t be a problem, nor a bad idea. Showing how an empty code block can be used as a scope block is good information and is often shown when teaching about block scopes.

2 Likes

Hi, thank you for this information. I do not know much about scope blocks yet, hence I was trying to clarify whether passing this test with a solution that is ‘not the proper way’ would lead to confusion or not (as I was confused in the beginning since in my solution, I just ignored a set of code which is supposed to be part of the solution, by setting the if statement into false). I was hoping maybe they improve the test before being able to pass, but as I mentioned before, this is really minor.

Your message on setting an empty code block and use it as a scope block is really informative, thank you! I havent tackled it yet, but I will be learning more about these things in the following days.