KISS JS

function destroyer(arr, …throwAway) {

  return arr.filter(elem => !throwAway.includes(elem));

}


destroyer([1, 2, 3, 1, 2, 3], 2, 3);

The above was a solution on Free Code Camp on their Javascript course, part of the ‘Advanced Algorithms’ section. The problem posed was:

You will be provided with an initial array as the first argument to the destroyer function, followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.

The function must accept an indeterminate number of arguments, also known as a variadic function. You can access the additional arguments by adding a rest parameter to the function definition or using the arguments object.

 I struggled with this one. The solution above was actually one I pilfered from the ‘help’ section where you can get the solution to the problem if need be. In this case (as is usually the case) there were actually several valid solutions, three to be exact. The other two were longer (like mine, ~ 10 lines or more) but then there was this one. One line. 

I’m currently reading a book, Clean Code’ by Robert C. Martin and I think the whole book could be summarized in one line, “Keep it short & sweet whenever possible”. Make your point clearly and elegantly then stop. 


Posted

in

by

Tags: