Global Scope Sky Lesson Problem


#1

This lesson:

https://www.codecademy.com/courses/learn-javascript-scope/lessons/scope/exercises/global-scope?action=lesson_resume&course_redirect=introduction-to-javascript

Here is the code that got me through Step 3 just fine:

const satellite = "The Moon";
const galaxy = "The Milky Way";

let stars = "North Star";

function myNightSky(){
  return "Night Sky: " + satellite + ", " + stars + ", " + galaxy;
}

Step 4 just asks me to write a console.log() line below the function that outputs the result of that to the console. So now my code looks like this:

const satellite = "The Moon";
const galaxy = "The Milky Way";

let stars = "North Star";

function myNightSky(){
  return "Night Sky: " + satellite + ", " + stars + ", " + galaxy;
}

console.log(myNightSky());

And the system is rejecting it saying, “Did you write a console.log() statement beneath the close of the function block?”

Um, yep, I sure did, and it’s clearly working as I’m getting the expected output in the console.

Help?


#2

Hi @faircloud64256,

I think the problem is not at step 4 of the lesson. It started at step 3.

Copied from the lesson step 3:

Below these variables, using const, write a function named myNightSky. Inside the function, include a return statement like this:

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

In Step 3, the lesson asked for using a const to write a function, that would mean using function expression instead of function declaration like what you’ve written above.

I think if you change how the function is written using const, you will pass Step 4 when you run it again. The console.log is fine.


For more reading about function declaration vs function expression, I’ve included some documentation links below. Essentially both ways are good to write a function, it’s just there’re some aspects that are different (hoisting is one of it).


Small note:

  • Thanks for using the markdown for code and passing the important information, it helps a lot.
  • I would suggest copy the quotation marks from the lesson as well, using single quotes. Sometimes, the validation might be very unforgiving, might want to eliminate all those possibilities of throwing out error.

Cheers :slight_smile:


#3

Scope is everything if you’re a variable. There is no jumping lines in this environment. It is also the concept that messes with lots of learners. We have to first envision enclosures within enclosures. Scope is described as a chain but each link is an enclosure. Start with that.


#4

Yep that’s what I missed, so really it should have stopped me a step earlier and noticed that I didn’t follow the directions to write the function expression correctly. Thanks!


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.