From c7b1908326ea6ba9984c9cde855611f2212ebc90 Mon Sep 17 00:00:00 2001 From: beliy-a Date: Tue, 9 Mar 2021 22:39:37 +0300 Subject: [PATCH 1/2] change linebreak-style --- .eslintrc.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From cfd6b35c00b88d3cbe1a012e14de60c977637fed Mon Sep 17 00:00:00 2001 From: beliy-a Date: Tue, 9 Mar 2021 22:40:09 +0300 Subject: [PATCH 2/2] add solving for all tasks --- Exercises/1-remove.js | 4 +++- Exercises/2-elements.js | 7 ++++++- Exercises/3-unique.js | 8 ++++---- Exercises/4-difference.js | 12 +++++++++--- 4 files changed, 22 insertions(+), 9 deletions(-) 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 };