Refactor Global Variables Out of Functions

Refactor Global Variables Out of Functions
0.0 0

#1

Tell us what’s happening:

I’ve added a variable to the function definition to hold the book list being passed in. In the searching I did, I found people were calling it bookList in the function definition and then assigning that to a variable defined within the function, but this has always worked for me to do the same thing without taking as many lines, kind of a variant on DRY. And since the book list array would be passed by value, it won’t modify the original copy of the list.

I’m getting an error message that list.indexOf(bookname) is not a function. Any idea what I’m missing here?

Your code so far


// 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 (list, bookName) {
  
  return list.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 (list, bookName) {
  console.log(list);
  if (list.indexOf(bookName) >= 0) {
    
    return list.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);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/refactor-global-variables-out-of-functions


#2

Your add function is returning the result of list.push(). push returns the new length of the array, not the array itself. You may want to do this instead:

list.push(bookName);
return list;

#3

Now working with this and I’m not even getting the errors any more. I’m not getting anything – the browser is hanging. Usually that’s a missing “:” or “}”, but I checked for those. (I know not everything is showing here, just between the //add your code here prompts.

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

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


#4

This is the error message I’m getting in my browser console –

Uncaught TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
at Object.r [as subscribeToResult] (subscribeToResult.js:74)
at t.error (catchError.js:111)
at t.notifyError (OuterSubscriber.js:22)
at t._error (InnerSubscriber.js:26)
at t.error (Subscriber.js:103)
at t._error (Subscriber.js:129)
at t.error (Subscriber.js:103)
at t._error (tap.js:95)
at t.error (Subscriber.js:103)
at t.notifyError (OuterSubscriber.js:22)


#5

I finally got it working. The key is that the array gets passed by reference, so even if you reassign it within the functions, they’re still copies of the original array. Which means you need to either use methods within your add and remove functions that won’t modify the array, or you need to manually copy the array item my item into a new array then work with that array.


#6

2 posts were split to a new topic: Adding to and removing strings from array doesn’t work