-3

Const pull = (arr, ...args) => {
    Let argState = Array.isArray(args[0] ? args[0] : args; Let pulled = arr.filter((v, i) => !argState.includes(v)); arr.length = 0; pulled.forEach(v => arr.push(v));
      return pulled;
    };
    Let arr1 = ['a', 'b', 'c', 'a', 'b', 'c'];
    Console.log(pull(arr1, 'arr1', 'c'))

Could any1 explain me how this code is working. Specially arrow functions. thank you.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
Amber Bdr
  • 1
  • 1
  • 2
    "Could any1 explain me how this code is working." — It doesn't work. It is riddled with unrecoverable errors. – Quentin Jun 23 '20 at 08:56
  • Ohhh. Sorry. Its mistakes.I should start javaScript keyword with lowercase and there were many pre defined objects in in this code like Array.isArray, arr.filter, nested arrow function, !argState.includes(v), and pulled.forEach. beacuse of this, i tought to get help from this community. Thank you for your help.. – Amber Bdr Jun 24 '20 at 01:27

1 Answers1

0

All of this can be googled:

  • Pull is a function

  • Think of a function as a blender with a container. You can keep using it again and again, you insert food (arguments) into the container (parameters).

  • For the purpose of understanding this example, imagine that a blender can have many containers. It's an industrial blender, whatever.

  • arr and args are the container (where the food will be put every time it's used)

  • an array is a type of a data structure. (arr - the person who wrote it probably wants to insert an array)

  • an array is a list. They are written with square brackets and separated by commas. Each item in an array has an index, starting at 0, NOT 1.

  • ...args is a restful parameter. That means that you can call the function (do 1 use of the blender), with an array or various items, although you only have one container. You leave that to later to decide.

  • They use a ternary operator. This is structured in the following way condition ? what to do if condition evaluates to true : what to do if condition evaluates to false

  • Array.isArray checks if something is an array or not. The individual checks if the first item of the array (list) that was inserted as an argument (food) in the parameter (container) args is an array (a list). if it is an array, it uses it. If not, it uses all the food (arguments) but in the container (parameter) args.

  • filter looks at the items in an array and returns ones that match a certain condition. This means that you might only like red skittles, so you search through a bag of skittles and filter out only the red ones. v represents every item on the list arr, and i represents the index. We break down the filtered 'condition', !argState.includes(v):

  • we use includes to check if anything in argState is the same as the value which has been inserted as the argument (food) in the first parameter (container), arr.value

  • This should return true or false. Since the ! is there, it will be reversed. If 'argState.includes(v)' returns true, '!argState.includes(v)' returns false, and viceversa

  • arr.length = 0 empties 'arr'.

  • every item made in pulled is cycled through and added to the empty 'arr' array. (Push adds an item to the end of an array, v represents every item in pulled)

  • since you return 'pulled', pulled will be printed when it is called and console.log'd.

  • You have the food/argument/list - arr1.

  • You call the function with 'arr1', the array/list as the food. The '...args' are just pure data, not inside any structure. You put console.log()

  • Within the parentheses you call the function (make 1 use of the blender)

  • You don't have to use console.log(), it just means that it will show in the console of the browser if all of your files are organised and let you test things.

  • You can go 'right-click/inspect' on your browser and click the 'console' tab to see an example of the console. you can code there and try things. maybe I'm missing something, but this code seems unusable.