FAQ: Scope - Blocks and Scope

This community-built FAQ covers the “Blocks and Scope” exercise from the lesson “Scope”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Blocks and Scope

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

How would i declare the function using arrow syntax?

e.g.
How do I convert

function aFunction(parameter){}

using "=> " ?

Got it.

Just incase someone might need the answer to this:

const city = ‘New York City’;
const logCitySkyline = () => {
let skyscraper = ‘Empire State Building’
return 'The stars over the ’ + skyscraper + ’ in ’ + city;
}
console.log(logCitySkyline());

11 Likes

I headed over here to find out why the following code does not compile:
const logCitySkyline = () => let skyscraper = 'Empire State Building';
I thought it might be the omission of the curly brackets ({}) but that should be permissible when the block consists of just one line of code (i.e. does not include the ‘return’ line).

However, both my code and digi_solutions’ does not seem to compile on codecademy, throwing the syntax error

SyntaxError: Unexpected identifier

Does this happen for anyone else? And are both codes wrong? Thanks!

Given that an implicit return is anticipated, the statement is incorrect since we cannot return an assignment. The function above does effectively nothing.

Say we have an object of skylines by city.

const skylines = {
    'New York City': 'Empire State Building',
    'Chicago': 'Sears Tower',
    'Seattle': 'Space Needle',
    'Toronto': 'CN Tower'
};
const get_city_skyline = city => skylines[city];

console.log(get_city_skyline('Toronto'));
// CN Tower
4 Likes

is that example of “array” method ?

1 Like

skylines is a plain object or more commonly referred to as, an object literal. If you have not yet reached the unit on objects, then push forward.

obj = {
    key: 'value'
}

To access the value, we subscript the key.

 obj['key']    //  value

Note that keys are ‘string’ type, so string literals are quoted. When using a variable, the typeof should be string.

prop = 'key'
obj[prop]    // value
1 Like

So, this seemed to be the solution.

const city = 'New York City'; {
  function logCitySkyline() {
  let skyscraper = 'Empire State Building';
  return 'The stars over the ' + skyscraper + ' in ' + city;
  }
  console.log(logCitySkyline());
};

When I use an arrow function, the code still works, however the tutorial says it is incorrect.

const city = 'New York City'; {
  const logCitySkyline = () => {
  let skyscraper = 'Empire State Building';
  return 'The stars over the ' + skyscraper + ' in ' + city;
  }
  console.log(logCitySkyline());
};

This may have been addressed, but the example does not mention curly brackets, so I’m not sure if I just misunderstood.

1 Like

Hello @9790220549.

Welcome to the forum.

Here is your code without the extra curly braces:

const city = 'New York City'; //the semicolon ends this statement

function logCitySkyline() { //this curly brace is the beginning of the function's code block
  let skyscraper = 'Empire State Building';
  return 'The stars over the ' + skyscraper + ' in ' + city;
} //this curly brace marks the end of the function's code block

console.log(logCitySkyline()); //This calls the console method '.log()' which subsequently calls the
//function 'logCitySkyline()`. The return value of 'logCitySkyline()' is the parameter to be printed
//to the console by the '.log()' method. 

6 Likes

Thank you for your quick response.

1 Like

Can someone explain the difference between what the lesson is telling us to do with using returns/console.log to print things vs this

function logCitySkyline(city, skyscraper)
{

console.log('The stars over the ’ + skyscraper + ’ in ’ + city)

}

logCitySkyline(‘NewYork’, ‘Empire State Building’)

 > function logCitySkyline(city, skyscraper) {
       console.log(`The stars over the ${skyscraper} in ${city}`)
   }
<- undefined
 > logCitySkyline('New York', 'Empire State Building')
   The stars over the Empire State Building in New York
<- undefined

The only return is undefined. The logging takes place inside the function, as the name suggests.

Let’s change the function to include a return value…

 > function logCitySkyline(city, skyscraper) {
       console.log(`The stars over the ${skyscraper} in ${city}`);
       return 1;
   }
<- undefined
 > logCitySkyline('New York', 'Empire State Building')
   The stars over the Empire State Building in New York
<- 1

Why does it matter what the return is though. In this scenario specifically we are not using it anywhere else

The why comes from design concerns. Do we just want to see the results, or do we wish to use them in our program?

1 Like

Guys, there are some sort of bug with:

“return 'The stars over the ’ + skyscraper + ’ in ’ + city;”

You have to delete spaces between the plus symbols like this:

return 'The stars over the ‘+skyscraper+’ in '+city;

The same problem appear in other exercises. So, keep this in mind.

1 Like

const city = (‘New York City’)

function logCitySkyline() {

let skyscraper = ‘Empire State Building’

return 'The stars over the ’ + skyscraper + ’ in ’ + city;

}

console.log(logCitySkyline())

In this block of code, is there a specific reason ‘New York City’ has to be specified above the function body? Could I instead use "let city = ‘New York City’ " in the function body(aka the curly brackets) ?

Thank you.

You could define city inside the function. Then it would be locally scoped. This lesson is trying to show you how scope works. Since city is declared outside of the function it is in the global scope. You can access a global variable outside of functions and inside functions. A variable defined inside of a function has local scope and can only be accessed inside of the function or inside of nested functions.

const city = (‘New York City’)  //global variable 

function logCitySkyline() {

let skyscraper = ‘Empire State Building’ //local variable

return 'The stars over the ’ + skyscraper + ’ in ' + city; // this has access to both the global and local variables

}

console.log(logCitySkyline()); // logs **The stars over the Empire State Building in New York City** 

console.log(city); //logs **New York City** city is a global variable so we have access to it outside of the function

console.log(skyscraper); // logs **undefined** skyscraper is a local scoped variable and is not available outside of the function

1 Like

There is no need to parenthesize the value since it has delimiters (quotes) that identify it as a string.

city = 'New York City'

As others have said, codecademy doesn’t accept the code, even when obtained from the solution codecademy gives. wtf

Hi everyone,

To give a bit of context to my dilemma. I stumbled across something by accident on step 2. When I enter in the function as so with the ‘city’ in the brackets:

const logCitySkyline = (city) => {}

Running the code accepts my syntax as the correct answer in steps 2 and 3.

However when I get to step 4 with the following:

const logCitySkyline = (city) => {
let skyscraper = ‘Empire State Building’;
return 'The stars over the ’ + skyscraper + ’ in ’ + city; }

I get this error:

Inside logCitySkyline did you return a string that includes skyscraper and city ? Make sure you don’t change the values of skyscraper and city

I know was with the adding the city to the brackets, on the other hand its got me thinking whether this is a bug in the code that checks the syntax or if there is a more advanced application of adding the variable to the brackets of the function.

Please also forgive my butchering of the jargon used in javascript, I’m still trying to get my head wrapped around the terminaology :slight_smile: