diff --git a/.eslintrc.json b/.eslintrc.json index 69eb1e4..aee727e 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -18,7 +18,7 @@ ], "linebreak-style": [ "error", - "unix" + "windows" ], "quotes": [ "error", @@ -265,4 +265,4 @@ "never" ] } -} +} \ No newline at end of file diff --git a/Exercises/1-remove.js b/Exercises/1-remove.js index 82fba07..a620173 100644 --- a/Exercises/1-remove.js +++ b/Exercises/1-remove.js @@ -1,7 +1,9 @@ 'use strict'; const removeElement = (array, item) => { - // Remove item from array modifying original array + array.forEach((el, i) => { + if (el === item) array.splice(i, 1); + }); }; module.exports = { removeElement }; diff --git a/Exercises/2-elements.js b/Exercises/2-elements.js index 8518c71..dd8a30e 100644 --- a/Exercises/2-elements.js +++ b/Exercises/2-elements.js @@ -1,7 +1,12 @@ 'use strict'; const removeElements = (array, ...items) => { - // Remove multiple items from array modifying original array + items.forEach((el) => { + const idx = array.indexOf(el); + if (idx !== -1) array.splice(idx, 1); + }); + + return array; }; module.exports = { removeElements }; diff --git a/Exercises/3-unique.js b/Exercises/3-unique.js index 7dd1012..ad4496c 100644 --- a/Exercises/3-unique.js +++ b/Exercises/3-unique.js @@ -1,8 +1,8 @@ 'use strict'; -// Create and return a new array without duplicate elements -// Don't modify initial array - -const unique = (array) => []; +const unique = (array) => { + const filtered = array.filter((el, i) => i === array.indexOf(el)); + return filtered; +}; module.exports = { unique }; diff --git a/Exercises/4-difference.js b/Exercises/4-difference.js index 37d24ab..5c5eaec 100644 --- a/Exercises/4-difference.js +++ b/Exercises/4-difference.js @@ -1,8 +1,14 @@ 'use strict'; -// Find difference of two arrays -// elements from array1 that are not includes in array2 -const difference = (array1, array2) => []; +const difference = (array1, array2) => { + const diffArr = []; + + array1.forEach((el) => { + if (!array2.includes(el)) diffArr.push(el); + }); + + return diffArr; +}; module.exports = { difference };