Seek and Destroy.44

Seek and Destroy.44
0

#1

What am i missing here , the if conditon works (i checked it) , but i am missing something, Help

Your code so far


function destroyer(arr) {
  // Remove all the values
  var args = Array.prototype.slice.call(arguments);
  let hna = args.splice(1 , 3);
  
  return  args[0].map(function(item,index){
   if(hna.includes(item)===true){
     delete item;
   }
   return args[0];
   
 })
 //console.log(hnak)
 
  

}

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

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/seek-and-destroy/


#2
destroyer([1,2,3,4,5,6], 1)
destroyer([1,2,3,4,5,6], 1, 2)
destroyer([1,2,3,4,5,6], 1, 2, 3)
destroyer([1,2,3,4,5,6], 1, 2, 3, 4)
destroyer([1,2,3,4,5,6], 1, 2, 3, 4, 5)
destroyer([1,2,3,4,5,6], 1, 2, 3, 4, 5, 6)

All of those are valid. Using your code, the first three will work, the next three will not. Any call of the destroyer function with more than three values to remove will fail.

Secondly, sure, you’re deleting items from the array. But delete can’t be used with arrays, it is for objects. delete only affects the specific property you applying to. Arrays have a property called length. So if you delete an item, that item does get deleted, but the length remains the same:

> let arr = [1,2,3,4]
> delete arr[0]
> arr
[null, 2, 3, 4]
> arr.length
4

#3

So i edited a couple of things:

function destroyer(arr) {
  // Remove all the values
  var args = Array.prototype.slice.call(arguments);
  let hna = args.slice(1);//edited
  /// let kilo instead of return 
  let kilo= args[0].map(function(item,index){
   if(hna.includes(item)===true){
    let hnak = args[0].splice(index, 1);//edited
   }
 })
return args[0];//placed it out of map function
}

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

#4

Use filter, not map, no need to use splice at all


#5

What’s wrong with splice?


#6

You’re mixing up paradigms. Map is functional, it takes an array and creates a new array, it should have no side effects. What you’re doing addition to that is modifying another array, it’s counter to how your supposed to use it. And the reason I say use filter is that what you’re trying to do with map and splice, filter will do that for you. You’re very, very close: the code will work, its just not quite using the tools in the way they’re supposed to be used


#7

Did it man , thank you

function destroyer(arr) {
  // Remove all the values
  var args = Array.prototype.slice.call(arguments);
  let hna = args.slice(1);//edited

 return args[0].filter(item=>
  !hna.includes(item)
 )

}

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

#9

But also wanted to ask you about that quote ,so you mean it is a bad convention to create side effects to any functional programming?


#10

As a rule, yes. Any function should ideally be referentially transparent: if you call it with the same input, it will always return the same output. If it has side effects, then it kinda cant be referentially transparent by definition. (Note JS is not a functional language per se, it’s just easy to write in a functional style)

Note you can’t really do a lot of useful stuff without side effects, so you do things like isolate all stateful stuff in one place to make it easier to control (this is what Redux does), or encase things in functional wrappers that behave predictably


#11

Right ,.thank you man