From 24619a55422abd18a77e93ca0af8f6b734097fac Mon Sep 17 00:00:00 2001 From: Jhon Mosk Date: Wed, 20 Sep 2023 22:27:30 +0300 Subject: [PATCH] labs done --- .prettierrc | 3 +++ Exercises/1-remove.js | 3 ++- Exercises/2-elements.js | 5 ++++- Exercises/3-unique.js | 11 ++++++++++- Exercises/4-difference.js | 7 ++++++- package.json | 2 +- 6 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..544138b --- /dev/null +++ b/.prettierrc @@ -0,0 +1,3 @@ +{ + "singleQuote": true +} diff --git a/Exercises/1-remove.js b/Exercises/1-remove.js index 82fba07..178d0b9 100644 --- a/Exercises/1-remove.js +++ b/Exercises/1-remove.js @@ -1,7 +1,8 @@ 'use strict'; const removeElement = (array, item) => { - // Remove item from array modifying original array + const index = array.indexOf(item); + if (index !== -1) array.splice(index, 1); }; module.exports = { removeElement }; diff --git a/Exercises/2-elements.js b/Exercises/2-elements.js index 8518c71..455af72 100644 --- a/Exercises/2-elements.js +++ b/Exercises/2-elements.js @@ -1,7 +1,10 @@ 'use strict'; const removeElements = (array, ...items) => { - // Remove multiple items from array modifying original array + for (const item of items) { + const index = array.indexOf(item); + if (index !== -1) array.splice(index, 1); + } }; module.exports = { removeElements }; diff --git a/Exercises/3-unique.js b/Exercises/3-unique.js index 7dd1012..c76cc17 100644 --- a/Exercises/3-unique.js +++ b/Exercises/3-unique.js @@ -3,6 +3,15 @@ // Create and return a new array without duplicate elements // Don't modify initial array -const unique = (array) => []; +const unique = (array) => { + const res = []; + + for (const item of array) { + const index = res.indexOf(item); + if (index === -1) res.push(item); + } + + return res; +}; module.exports = { unique }; diff --git a/Exercises/4-difference.js b/Exercises/4-difference.js index 37d24ab..c0087f0 100644 --- a/Exercises/4-difference.js +++ b/Exercises/4-difference.js @@ -3,6 +3,11 @@ // Find difference of two arrays // elements from array1 that are not includes in array2 -const difference = (array1, array2) => []; +const difference = (array1, array2) => + array1.filter((item) => { + const index = array2.indexOf(item); + if (index === -1) return item; + return false; + }); module.exports = { difference }; diff --git a/package.json b/package.json index f8bd580..53443b6 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "author": "Timur Shemsedinov ", "license": "MIT", "scripts": { - "test": "eslint ./Exercises; hpw", + "test": "eslint ./Exercises && hpw", "ci": "eslint ./Exercises && hpw" }, "dependencies": {