简体   繁体   中英

How can I check this function for truthy or return an empty array if falsy?

I am looking at this assignment, and my question is about the part in bold at the end:

 // Do not edit the code below. var myGroceryList = ['chips', 'pizza', 'hotpockets', 'MtnDew', 'corndogs']; // Do not edit the code above.

Here we're going to write a function that mimics going shopping and checking things off of our grocery list and adding new items to our list.

Write a function called removeItem that is given two arguments, the first is myGroceryList , and the second is an item to remove from myGroceryList . If the second argument (or the item to add or remove) matches an item in myGroceryList , remove that item from the your grocery list and return the new, updated grocery list.

Once you do that, write another function called addItem that is given two arguments, the first is myGroceryList and the second is an item to add to your grocery list. In addItem add the item you passed in to myGroceryList then return the new, updated grocery list.

In both removeItem and addItem check to see if the 'myGroceryList' and 'item' arguments are truthy. If they are not, return an empty array.

Here are some examples of calling your functions and what should be returned:

 removeItem(myGroceryList, 'chips') --> ['pizza', 'hotpockets', 'MtnDew', 'corndogs']; addItem(myGroceryList, 'Jerky') --> ['pizza', 'hotpockets', 'MtnDew', 'corndogs', 'Jerky']; removeItem(myGroceryList) --> []; addItem() --> [];

Here is my code:

removeItem=(myGroceryList,item)=>{
   return myGroceryList.filter((thing)=>{
      return thing != item
   })
}

addItem=(myGroceryList, item)=>{
   myGroceryList.push(item);
   return myGroceryList;
} 

How can I get the final step of this to work?

Simply add an if that verifies the truthiness of both arguments:

removeItem=(myGroceryList,item)=>{
   if (!myGroceryList || !item) return [];
   return myGroceryList.filter((thing)=>{
      return thing != item
   })
}

Not mutating the argument

As you are asked to return the new list, you did well to use filter and not mutate the original list. But you should better apply the same principle to addItem , which also is supposed to return the new list, and so it is better not to mutate the list that was given:

addItem=(myGroceryList, item)=>{
   if (!myGroceryList || !item) return [];
   return myGroceryList.concat(item); // don't use push, but concat
} 

Or... mutating the argument?

However, the example code that is given at the end of the assignment, does not use the return value of the function (except the console reporting it), and from the second call we can see that "chips" has actually been removed from the list that was given to the first call of removeItem .

So that means you have to make your functions having side-effects .

In that case you should not use filter or concat , but change the first function to use splice :

removeItem=(myGroceryList,item)=>{
   if (!myGroceryList || !item) return [];
   let i = myGroceryList.indexOf(item);
   if (i >= 0) myGroceryList.splice(i, 1); // mutate the list
   return myGroceryList;
}

...and you would use your version with push for addItem :

addItem=(myGroceryList, item)=>{
   if (!myGroceryList || !item) return [];
   myGroceryList.push(item); // mutate...
   return myGroceryList;
} 

Instead of checking if truthy, you can check if falsy and return an empty array right away.

   addItem=(myGroceryList, item) => {
      if(!myGroceryList) { return [] }
      myGroceryList.push(item);
      return myGroceryList;
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM