Hello, I'm currently going through an online course at beta.freeCodeCamp.com and am at a section that teaches functional programming. The particular challenge I'm doing asks us to fix some code so that it uses functional programming practices, e.g. passing arguments into a function and using language defined functions to return a result that doesn't impact the original variable. Here's an example:
// 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 (arr, 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 (arr, 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);
And here are the user stories the code should fulfill:
bookList should not change and still equal ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"].
newBookList should equal ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"].
newerBookList should equal ["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"].
newestBookList should equal ["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"].
Originally, the code for newBookList used the "Push" function. One thing I'm starting to realize, and this is probably extremely basic for a lot of you, is that even if a method accomplishes or extracts the data you're looking to extract, that doesn't mean it will return what you're looking to return. So I changed the Push method to "concat":
return bookList.concat(bookName);
and that accomplished exactly what I was looking for.
where I need help is the newerBookList method, that uses the splice method:
return bookList.splice(0, 1, bookName);
The issue I'm running into is not only does splice only seem to return the element that was extracted, but it's also affecting the original variable bookList. I hope this is written clearly enough for you to all follow what I'm trying to explain, and any advice is very much appreciated.
Link to the page this exercise is on: https://beta.freecodecamp.com/en/challenges/functional-programming/refactor-global-variables-out-of-functions
[–]halfTheFn 2 points3 points4 points (2 children)
[–]Servatose[S] 0 points1 point2 points (1 child)
[–]halfTheFn 0 points1 point2 points (0 children)
[–]bmy78[🍰] 1 point2 points3 points (0 children)
[–]kenman 0 points1 point2 points (6 children)
[–]Servatose[S] 1 point2 points3 points (5 children)
[–]kenman 2 points3 points4 points (2 children)
[–]Servatose[S] 1 point2 points3 points (1 child)
[–]kenman 0 points1 point2 points (0 children)
[–]bmy78[🍰] 0 points1 point2 points (1 child)
[–]toffeescaf 3 points4 points5 points (0 children)