Skip to content

Tasks done #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Exercises/1-remove.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const removeElement = (array, item) => {
// Remove item from array modifying original array
};
const removeElement = (array, item) =>
(array.includes(item) ? array.splice(array.indexOf(item), 1) : array);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove ternary to improve readability.


module.exports = { removeElement };
3 changes: 2 additions & 1 deletion Exercises/2-elements.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

const removeElements = (array, ...items) => {
// Remove multiple items from array modifying original array
for (const item of items)
array.includes(item) ? array.splice(array.indexOf(item), 1) : array;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove ternary to improve readability.

};

module.exports = { removeElements };
10 changes: 9 additions & 1 deletion Exercises/3-unique.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
// Create and return a new array without duplicate elements
// Don't modify initial array

const unique = array => [];
const unique = array => {
const na = [];

array.forEach(i => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use for..of here, it's much faster and suitable in this case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I need to use Array functions mostly, so decided to use forEach

if (!na.includes(i)) na.push(i);
});

return na;
};

module.exports = { unique };
10 changes: 9 additions & 1 deletion Exercises/4-difference.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
// Find difference of two arrays
// elements from array1 that are not includes in array2

const difference = (array1, array2) => [];
const difference = (array1, array2) => {
const na = [];

array1.forEach(i => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use for..of here.

if (!array2.includes(i)) na.push(i);
});

return na;
};

module.exports = { difference };