Adding to and removing strings from array doesn't work

Could someone tell me where I am wrong ?

function add (list,bookName) {
   list=bookList.slice();
   list.push(bookName);
   return list;
}

function remove (list,bookName) {
  list=bookList.slice();
    let n=list.indexOf(bookName);
    if(n>=0){
     list.splice(n,1);
    return list;
    }
}

var newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');

At the end newerBookList equals
["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]

instead of
["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"].

I think those functions are supposed to modify the array passed to them, so you shouldn’t do list=bookList.slice(). Your remove function ends up modifying a copy of the bookList array, not the array that’s returned from the add() call.

PS. If you have questions, please create a new thread. You’ll more likely to get responses that way.

Great thank you for your help! Solved it :slight_smile: