Challenge Local Scope and Functions has an issue.
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36.
Please describe how to reproduce this issue, and include links to screenshots if possible.
My code:
function myLocalScope() {
myVar = 'use strict';
console.log(myVar);
}
myLocalScope();
// Run and check the console
// myVar is not defined outside of myLocalScope
console.log(myVar);
// Now remove the console log line to pass the test
This Challenge asks the user to make a variable..
1) In it's initialized state there is the line "use strict"; So the assumption I made was that the instruction is to assign the string to myVar.
2) Fine. that's what I did, then it looks like the goal is to run it, see it fail (because the last line is a call to print variable "myVar" which will not work because it is only a local variable within the function.
3) Then the "commented out" instructions say to comment that line out. My expectation would be that then the illegal call would no longer be there and the challenge would pass! But this is not what happens!!! Instead it just gives 2 errors: 'no global variable "myVar" ' What does that Mean???? Nowhere in the commented out instructions, or Challenge instructions does it ask to make a global variable!!!
Note that even if I DO create a global variable .. the challenge does not pass either!
So what is required to make this challenge pass? Perhaps the instructions for this challenge are not clearly stated?
c

You never set up myVar with a var in front of it in the first part, but the code from the image is 100% correct. What's the problem?
@aolyf thank you for trying to help, but your advice is not correct.
@JeffGreenlee42 'use strict'; is a way of enabling strict mode in javaScript, more details on this here
Please reset the challenge and follow the instructions. If the challenge still doesn't pass, use the hint provided and refresh the challenge. If the challenge still fails to pass, please use the Help button.
Happy Coding!
We need to make this challenge more clear.
Can we have a PR submitted to edit the challenge seed to produce:
function myLocalScope() {
'use strict'; // you shouldn't need to edit this line
console.log(myVar);
}
myLocalScope();
// Run and check the console
// myVar is not defined outside of myLocalScope
console.log(myVar);
// Now remove the console log line to pass the test
This should be the initial code the camper sees when visiting the challenge for the first time or reseting the challenge.
@Bouncey .. Thanks for understanding my confusion!!! I was not aware of utilization of 'use strict'... so to my JS newbie eyes (I confess I'm "old school" and a bit biased by my experience typed languages) the code was totally confusing.. Since it looked like a string just sitting by its lonesome self on a separate line in the function, it looked like it sorely needed assignment! Since there was no prior explanation of "use strict" , I can't imagine I wasn't the only one tripped up by this. :) (Thank you so much for the link to its documentation!). I see that you suggested putting the comment "you shouldn't need to edit this line"... which helps. Still, perhaps some brief explanation might help enlighten the newbie on why this odd statement is necessary.... or at lease "we will explain in a later challenge".
So just to describe again my user experience when I struggled a bit with this exercise:
My understanding was that the instruction was intended for the user to attempt the call to console.log on the variable in it's global context.. and then they would see the error displayed (since there is no global variable). Then the user would comment out (as instructed) that call and then test again - and the challenge test would pass. Please correct me if my understanding is incorrect?
This was my expectation of what was required for the challenge. Much to my surprise (and consternation!) this was NOT how it transpired. After seeing the expected error, then commenting out the console call, It still produced the two errors! So from my perspective, I seemed to be at a dead end! I was damned if I do, damned if I don't. After struggling with it, trying out things.. even adding in extra code... I was stuck! So that's what prompted me to create this post. Then finally after resetting the whole exercise several times.. it suddenly passed ! I have no idea why it worked the final time.. I did not do anything different the final time. Though I suspect the 'use strict' statement had something to do with it.
@Bouncey can I be assigned for this issue?
I know I'm not a first-timer but I only have 4 PR on github so far.
I've had the same problem as Jeff.
It worked when I performed a hard refresh in the browser so I'm guessing it is the cache issue.
Was struggling with this for so long.. did a refresh like @tomosuto and worked like a charm.
Thanks!
Just want to add that the previous challenge makes it even more confusing because it doesn't specify to declare the variable with the "var" keyword", explicitly stating that when anytime you declare a variable with "var", it will have a global scope.
I also had to refresh to get this challenge to pass without any change to the code. This one definitely needs updating, and maybe consider explaining when to use "var" and when not to in a way that isn't utterly confusing.
`
function myLocalScope() {
'use strict';
console.log(myVar);
}
myLocalScope();
// Run and check the console
// myVar is not defined outside of myLocalScope
console.log(myVar);
// Now remove the console log line to pass the test`
@staffordr I posted the current code I have. I did a hard refresh on my Chrome browser and still this fails. Any ideas. I'm lost.