[Beginner] Functional Programming help


[Beginner] Functional Programming help

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: http://ift.tt/2usD6cV

Submitted August 11, 2017 at 01:14AM by Servatose
via reddit http://ift.tt/2uKFx5J

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s