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] Bug in seed code for "Functional Programming: Refactor Global Variables Out of Functions"

See original GitHub issue

Challenge refactor-global-variables-out-of-functions has an issue.

According to the instructions, the goal of this challenge is to

Refactor (rewrite) the code so the global array bookList is not changed inside either function.

Another requirement (which doesn’t seem to be be tested for) is that

Both functions should return an array

The problem is that the initial challengeSeed doesn’t meet the second requirement since neither Array.push() nor Array.splice() returns the updated array. Since the challenge’s intention seems to be to “Refactor Global Variables Out of Functions”, I think this needs to be fixed, so campers can focus on removing global variables rather than making the code work.

To resolve these issues, I think we should

  • Make sure the functions in the challengeSeed returns the updated array, as their doc strings says.
  • Possibly add tests to verify that the functions do return an array.

// the global variable
var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];

/* This function should add a book to the list and return the list */
// New parameters should come before the bookName one

// Add your code below this line
function add (bookName) {
  
  return bookList.push(bookName);
  
  // Add your code above this line
}

/* This function should remove a book from the list and return the list */
// New parameters should come before the bookName one

// Add your code below this line
function remove (bookName) {
  if (bookList.indexOf(bookName) >= 0) {
    
    return bookList.splice(0, 1, bookName);
    
    // Add your code above this line
    }
}

var newBookList = add(bookList, 'A Brief History of Time');
var newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
var newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');

console.log(bookList);

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
Greenheartcommented, Feb 15, 2017

I really like the idea of only using local variables in functions, but it feels like this challenge is trying to do too much at one time.

Maybe we could split the concepts into two smaller challenges:

1. Refactor Global Variables out of Functions

  • Use similar challengeSeed to what we have now.
  • The goal is to replace the global variable bookList with a function-parameter (that campers decide the name for).

2. Avoid Mutating External Variables

  • Builds upon the challengeSeed from the previous challenge.
  • The goal is to teach why and how to avoid modifying variables you get passed to your functions.
  • In this case, this could be about using Array.prototype.slice() to create a copy before changing any data in the array with books.

What do you think? 😊

1reaction
BurnhamGcommented, Oct 18, 2018

I’ve been working on fixing this and should have a pull request in by early next week at the very latest.

On Wed, Oct 17, 2018, 17:25 Rabindranath Ferreira notifications@github.com wrote:

any advance with this i try 2 solutions using local variables inside off function and does’t work, and when i try to use slice or indexOf method for arrays the console told me “this xxxx is not a function”

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/freeCodeCamp/freeCodeCamp/issues/13157#issuecomment-430792340, or mute the thread https://github.com/notifications/unsubscribe-auth/ARZO-P5c81LvOM0dh_MbGKTgTVgTIZiOks5ul6BWgaJpZM4L3gIY .

Read more comments on GitHub >

github_iconTop Results From Across the Web

FreeCodeCamp -Refactor Global Variables Out of Functions
[beta] Bug in seed code for "Functional Programming: Refactor Global Variables Out of Functions" Challenge refactor-global-variables-out-of- ...
Read more >
Refactor Global Variables Out of Functions - Free Code Camp
In this functional programming tutorial we refactor global variables out of functions. This video constitutes one part of many where I cover ...
Read more >
Functional Programming: Refactoring Global Variables Out of ...
Let's rewrite the code so the global array bookList is not changed inside either function. The add function should add the given bookName...
Read more >
John Carmack on Inlined Code | Hacker News
His main concern with the previous approach is that functions relied on outside-the-function context (global or quasi-global state is ...
Read more >
OCaml Programming: Correct + Efficient + Beautiful
past title of this book was “Functional Programming in OCaml”. ... you off to Binder, it will modify the code cells on the...
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