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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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());
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
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.
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.
> 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
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) ?
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
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