[beta] Bug in seed code for "Functional Programming: Refactor Global Variables Out of Functions"
See original GitHub issueChallenge 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:
- Created 7 years ago
- Comments:12 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
challengeSeed
to what we have now.bookList
with a function-parameter (that campers decide the name for).2. Avoid Mutating External Variables
challengeSeed
from the previous challenge.Array.prototype.slice()
to create a copy before changing any data in the array with books.What do you think? 😊
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: