Error? 3. Global Scope - step 2

Please tell me what is wrong with the following code when I attempt to complete step 2 of Part 3. Global Scope of the Scope lesson in Learn Javascript. Thanks.

The code compiles correctly. The text returned was copied from the exercise.

URL: https://www.codecademy.com/courses/introduction-to-javascript/lessons/scope/exercises/global-scope

Tutorial Task: Below the variables created in the previous step, write a function named callMyNightSky. Inside the function, include a return statement like this:

**return 'Night Sky: ' + satellite + ', ' + stars + ', and ' + galaxy;**

My attempt 1:`

**callMyNightSky = () => **
**'Night Sky: ' + satellite + ', ' + stars + ', and ' + galaxy;**

Compile error message (on Run): None
Tutorial error message: Oops! The test returned an error. Maybe you have a syntax error, or a typo. See full error.


My attempt 2:

function callMyNightSky () {return 
'Night Sky: ' + satellite + ', ' + stars + ', and ' + galaxy;
}
'''
Compile error message (on Run): None
Tutorial error message: Oops! The test returned an error. Maybe you have a syntax error, or a typo. See full error.

**********************************************************************

Hello, and welcome to the forums!

Try putting return on the same line as the rest of what you want returned in Attempt #2. JavaScript doesn’t require the ; so when it reached the end of the line with return, it saw it as complete and executed the return without evaluating the rest of the code.

Scott,

Many thanks for your prompt reply – but no change to the result (i.e code compiles but Tutorial generates same error (Appendix B)).

i) placed return on same line as what I wanted returned (Appendix A); and

ii) deleted the “;” at the end of the line (Appendix A)

Vince

Appendix A – Attempt 2 code changed per Scott’s recommendations

function callMyNightSky () {

return 'Night Sky: ’ + satellite + ', ’ + stars + ', and ’ + galaxy

}

Appendix B – Tutorial error message

Oops! The test returned an error. Maybe you have a syntax error, or a typo. See full error.

Post the full source here, including the global variables, so we can check it out. I used an arrow function, but I know the hint says you can define the function using any format. Maybe there is something else that might shed some light on it.

Be sure to use this so we can see the formatting without the forum trying to change quotes around and such:

image

1 Like

Scott,

Additional info:

Appended Code:

console.log(callMyNightSky())

displays:

Night Sky: The Moon, North Star, and The Milky Way

But Tutorial still says:

Oops! The test returned an error. Maybe you have a syntax error, or a typo. See full error.

The tests can be stricter than what is running the code, so it’s possible to get output, but the test could fail and sometimes give a generic message. I’ve seen it before in other parts of the course when a keyword like let or const wasn’t used. JavaScript can still use variables like that, but the tests could fail. That’s why I’m curious to see your full source code.

Attempt 1 (Arrows):

satellite = ‘The Moon’;

galaxy = ‘The Milky Way’;

stars = ‘North Star’;

callMyNightSky = () => 'Night Sky: ’ + satellite + ', ’ + stars + ', and ’ + galaxy

console.log(callMyNightSky())

Attempt 2 (Function declaration):

satellite = ‘The Moon’;

galaxy = ‘The Milky Way’;

stars = ‘North Star’;

function callMyNightSky () {

return 'Night Sky: ’ + satellite + ', ’ + stars + ', and ’ + galaxy

}

console.log(callMyNightSky())

Your global variables weren’t declared with const or let, so I think that the test is failing. JavaScript allows it, but the test may need it. It’s also a best practice to use one of them.

Look at the example in the exercise description at the top. Notice it declared color with const. You can also look at the hint for task #1 where it suggests you use const for satellite, galaxy, and stars because the variables won’t be reassigned.

Scott,

Many thanks – now fixed.

Tutorial PASSES step 2 if:

i) Function declaration used; and

ii) ‘const’ or ‘let’ to declare global variables

Tutorial FAILS step 2 if:

i) Arrows used; or

ii) Global variables not declared with anything

Recommendation:

Training documentation changed on next release to reflect this (not urgent).

Best wishes,

Vince

Attempt v2.1 (function declaration; uses ‘const’)

Result: PASS

const satellite = ‘The Moon’;

const galaxy = ‘The Milky Way’;

const stars = ‘North Star’;

function callMyNightSky () {

return 'Night Sky: ’ + satellite + ', ’ + stars + ', and ’ + galaxy

}

console.log(callMyNightSky())

Attempt 2.2 (function declaration,; uses ‘let’)

Result: PASS

let satellite = ‘The Moon’;

let galaxy = ‘The Milky Way’;

let stars = ‘North Star’;

function callMyNightSky () {

return 'Night Sky: ’ + satellite + ', ’ + stars + ', and ’ + galaxy

}

console.log(callMyNightSky())

Attempt v1.2 (arrows, uses ‘const’)

Result: FAIL

const satellite = ‘The Moon’;

const galaxy = ‘The Milky Way’;

const stars = ‘North Star’;

callMyNightSky = () => 'Night Sky: ’ + satellite + ', ’ + stars + ', and ’ + galaxy

console.log(callMyNightSky())

Attempt v1.3 (arrows, uses ‘let’)

Result: FAIL

let satellite = ‘The Moon’;

let galaxy = ‘The Milky Way’;

let stars = ‘North Star’;

callMyNightSky = () => 'Night Sky: ’ + satellite + ', ’ + stars + ', and ’ + galaxy

console.log(callMyNightSky())

You’re welcome. Nice job passing it.

Just for clarification, the likely reason your arrow function tests failed is because they also need a let or const with that notation. You can see the example in the exercise description at the top. Had you used let or const with callMyNightSky, then all of your tests should have passed.

Attempt 1.4 (Arrows; const on parameters and const on function)

Result: PASS

const satellite = ‘The Moon’;

const galaxy = ‘The Milky Way’;

const stars = ‘North Star’;

const callMyNightSky = () => 'Night Sky: ’ + satellite + ', ’ + stars + ', and ’ + galaxy

console.log(callMyNightSky())