diff --git a/Exercises/1-remove.js b/Exercises/1-remove.js index 82fba07..8104eaa 100644 --- a/Exercises/1-remove.js +++ b/Exercises/1-remove.js @@ -1,7 +1,10 @@ 'use strict'; const removeElement = (array, item) => { - // Remove item from array modifying original array + const index = array.indexOf(item); + if (index !== -1) { + array = array.splice(index, 1); + } }; module.exports = { removeElement }; diff --git a/Exercises/2-elements.js b/Exercises/2-elements.js index 8518c71..4c98fcf 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 + for (const item of items) { + if (array.includes(item)) { + const index = array.indexOf(item); + array.splice(index, 1); + } + } }; module.exports = { removeElements }; diff --git a/Exercises/3-unique.js b/Exercises/3-unique.js index b738823..c56ff56 100644 --- a/Exercises/3-unique.js +++ b/Exercises/3-unique.js @@ -3,6 +3,12 @@ // Create and return a new array without duplicate elements // Don't modify initial array -const unique = array => []; +const unique = array => { + const resArr = []; + for (const arg of array) { + if (!resArr.includes(arg)) resArr.push(arg); + } + return resArr; +}; module.exports = { unique }; diff --git a/Exercises/4-difference.js b/Exercises/4-difference.js index 37d24ab..4bbdb49 100644 --- a/Exercises/4-difference.js +++ b/Exercises/4-difference.js @@ -3,6 +3,12 @@ // Find difference of two arrays // elements from array1 that are not includes in array2 -const difference = (array1, array2) => []; +const difference = (array1, array2) => { + const resArr = []; + for (const arg1 of array1) { + if (!array2.includes(arg1)) resArr.push(arg1); + } + return resArr; +}; module.exports = { difference };