Functional Programming - Refactor Global Variables Out of Functions

Tell us what’s happening:
Describe your issue in detail here.
I need to understand why I’m getting undefined when i try to console.log(remove()) yet i still pass the task. What am i not getting right?

  **Your code so far**
// The global variable
const bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];

// Change code below this line
function add(arr, bookName) {
let newArr = [...arr]
newArr.push(bookName);
return newArr;

// Change code above this line
}
console.log(add(bookList,"Pebbles along the Riverbank"))

// Change code below this line
function remove(arr, bookName) {
let newArr = [...arr]
const book_index = newArr.indexOf(bookName);
if (book_index >= 0) {

  newArr.splice(book_index, 1);
  return newArr;

  // Change code above this line
  }
}

console.log(remove(bookList, "Pebbles along the Riverbank"))
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0

Challenge: Functional Programming - Refactor Global Variables Out of Functions

Link to the challenge:

Your remove does not have a return statement for when the title is not in the array, and Pebbles along the Riverbank is not in bookList, so it returns undefined

If I understand the question correctly, the undefined belongs to the console.log() statement, unless your function is actually returning undefined (you should get 2 undefined then)

console.log() always logs the value passed - if any - and then in most javascript runtimes the return value itself is logged out.

So for example, in Node JS we can write:

function a(){ 
  /* 
  no return i.e implicit undefined, 
  logs undefined 
  */ 
}

// or

function a(){ 
  /* logs 5 when executed */
  return 5
 }

And both cases log the return value, which in the first case, as any implicit return value, it is assigned undefined.

I noticed that after sending the message. Thank you all so much.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.