question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[beta] Improve challengeSeed for "ES6: Scopes and the var keyword"

See original GitHub issue

ES6: Scopes and the var keyword

Issue Description

The editor shows an error for line 6 of the initial challengeSeed, stating that we shouldn’t create functions within loops. We can avoid this by moving this function definition out of the loop and only reference it in the loop.

Updated challengeSeed:

var newCampers = [{camper: "Wil"}, {camper: "Sam"}, {camper: "Dav"}];

function roleCall (i) {
  return "Camper # " + (i + 1) + " has arrived.";
} 

// only change code below this line
for (var i = 0; i < newCampers.length; i++) {
// only change code above this line
  newCampers[i].roleCall = roleCall.bind(null, i);
}

console.log(newCampers[0].roleCall());
console.log(newCampers[1].roleCall());
console.log(newCampers[2].roleCall());

However, this also means we lose the closure that gives us access to i. Since code isn’t supposed to be written like this anyway, I think we’re better of by using roleCall.bind(null, i), despite the increased complexity. The code above works just like the previous example, but uses better (if not best) practices.

Even better, @BrendanSweeny suggested this could further be improved:

var newCampers = [{camper: "Wil"}, {camper: "Sam"}, {camper: "Dav"}];

function roleCall (i) {
  return "Camper # " + (i + 1) + " has arrived.";
} 

// only change code below this line
for (var i = 0; i < newCampers.length; i++) {
// only change code above this line
  newCampers[i].roleCall = roleCall.bind(null, i);
}

console.log(newCampers[0].roleCall());
console.log(newCampers[1].roleCall());
console.log(newCampers[2].roleCall());
console.log(typeof i);

By adding a console.log(), the camper can see that using let will cause typeof i on the last line to be "undefined".

@BrendanSweeny also had an idea about adding an extra test to verify that i ===. I think this is a good idea.

Building upon Brendan’s test case from https://github.com/freeCodeCamp/freeCodeCamp/pull/12956#discussion_r98820528, this could be what we’re looking for.

assert.isUndefined(i, 'message: <code>i</code> should not be available outside of the <code>for</code> loop.');

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
BrendanSweenycommented, Feb 4, 2017

Absolutely! I’ll create a PR sometime this weekend.

0reactions
Greenheartcommented, Feb 4, 2017

@HKuz It’s not introduced anywhere (AFAIK) 😢 It’s a really nice language feature.

If we want to use the simpler example, we’d need to update both the seed as well as the instructions. @BrendanSweeny Would you like to create the PR for this, as you came up with the solution? ☺️

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compare Scopes of the var and let Keywords - Free Code Camp
In this JavaScript ES6 tutorial we compare scopes of the var and let keywords. This is one part of a multi-part series where...
Read more >
ES6 Lesson: Compare Scopes of the var and let Keywords
Certification: JavaScript Algorithms and Data StructuresCourse: ES6Lesson: Compare Scopes of the var and let KeywordsfreeCodeCamp tutorialIn ...
Read more >
Compare Scopes of the var and let Keywords, ES6 ... - YouTube
In this lesson from the Introduction to ES6 Challenges on freeCodeCamp we look further into the use cases and functionality of the "'let"' ......
Read more >
Learn ES6 (2/31) | Compare Scopes of the var and let Keywords
Learn ES6 (2/31) | Compare Scopes of the var and let Keywords | freeCodeCampWhen you declare a variable with the var keyword, it...
Read more >
Variable Scope in Modern JavaScript with var, let and const
I regularly find people declaring variables with var in the middle of code, not knowing how JavaScript will scope these.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found