Misleading tests

Misleading tests
0

#1

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/local-scope-and-functions

The logs of this task are misleading, and should be improved.

The code:


function myLocalScope() {

‘use strict’; // you shouldn’t need to edit this line

var myVar=“hi”;

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

Logs when you run the tests


// running tests

myVar is not defined

myVar is not defined

// tests completed

The problem is that there are two “myVar is not defined” outputs after the tests.
So one might conclude that both the local and the global variables are undefined.
But as the above code shows, the local variable is defined, so these results are related only to the global variable. So a student would search for a mistake that does not exist.
Such behavior is misleading and should be corrected.


#2

myVar is not defined when you attempt to console.log it in myLocalScope (as stated in the comment). That is the whole point of this exercise.


#3

The thing is that, in the code I posted myVar is defined in myLocalScope.

function myLocalScope() {

‘use strict’; // you shouldn’t need to edit this line

var myVar=“hi”;

console.log(myVar);

}

myLocalScope();

But still tests give two “myVar is not defined” results, which I find very misleading.


#4

The whole point of this lesson is that the variable is locally scoped to to function. It is defined inside the function. It is not defined outside of the function. When console.log(myVar) is called outside of the function, you will get an error.

The message that you are seeing, “myVar is not defined”, is not coming from the Free Code Camp testing suite. That is an actual error that is being generated by JavaScript because you are attempting to use a variable that is not defined.


#5

Thank you for the explanation.
Now it makes sense to me.