Skip to content

Lab7 Arrays #16

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
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"linebreak-style": [
"error",
"unix"
"windows"
],
"quotes": [
"error",
Expand Down
7 changes: 5 additions & 2 deletions Exercises/1-remove.js
Original file line number Diff line number Diff line change
@@ -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.splice(index, 1);
};

const array = [1, 2, 3, 4, 5, 6, 7];
removeElement(array, 5);
console.log(array);
module.exports = { removeElement };
9 changes: 7 additions & 2 deletions Exercises/2-elements.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
'use strict';

const removeElements = (array, ...items) => {
// Remove multiple items from array modifying original array
for (let i = 0; i < items.length; i++) {
const index = array.indexOf(items[i]);
if (index !== -1) array.splice(index, 1);
}
};

const array = ['Kiev', 'Beijing', 'Lima', 'Saratov'];
removeElements(array, 'Lima', 'Berlin', 'Kiev');
console.log(array);
module.exports = { removeElements };
16 changes: 11 additions & 5 deletions Exercises/3-unique.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
'use strict';

// Create and return a new array without duplicate elements
// Don't modify initial array

const unique = (array) => [];

const unique = (array) => {
const result = [];
for (const str of array) {
if (!result.includes(str)) {
result.push(str);
}
}
return result;
};
const result = unique([2, 1, 1, 3, 2]);
console.log(result);
module.exports = { unique };
17 changes: 12 additions & 5 deletions Exercises/4-difference.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
'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 array = [];
for (let i = 0; i < array1.length; i++) {
const item = array1[i];
if (!array2.includes(item)) array.push(item);
}
return array;
};
const array1 = [7, -2, 10, 5, 0];
const array2 = [0, 10];
const result = difference(array1, array2);
console.log(result);
module.exports = { difference };
Loading