Skip to content

Labs done #8

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
3 changes: 2 additions & 1 deletion Exercises/1-remove.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

const removeElement = (array, item) => {
// Remove item from array modifying original array
const index = array.indexOf(item);
return index !== -1 ? array.splice(index, 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.

This task implies modifying original array, no need to return anything, but it's ok. Not ok is to use ternary here, if statement is more relevant for this case.

};

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

const removeElements = (array, ...items) => {
// Remove multiple items from array modifying original array
for (const i of items) {
Copy link
Member

Choose a reason for hiding this comment

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

Naming i may confuse someone (may refer to i:Number like in regular for loops), better use item or element here.

const index = array.indexOf(i);
if (index !== -1) array.splice(index, 1);
}
return array;
Copy link
Member

Choose a reason for hiding this comment

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

No need to return, but it's ok.

};

module.exports = { removeElements };
8 changes: 7 additions & 1 deletion Exercises/3-unique.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 arr = [];
for (const item of array)
Copy link
Member

Choose a reason for hiding this comment

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

Use code block here.

if (!arr.includes(item)) arr.push(item);
return arr;
};

module.exports = { unique };

8 changes: 7 additions & 1 deletion Exercises/4-difference.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 arr = [];
for (const item of array1) {
if (!array2.includes(item)) arr.push(item);
}
return arr;
};

module.exports = { difference };