Skip to content

Commit 714f1bc

Browse files
author
Elena Kononchuk
committed
repo with tasks to practice HowProgrammingWorks#7
1 parent 5f15fdd commit 714f1bc

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

Exercises/1-remove.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
'use strict';
1+
"use strict";
22

33
const removeElement = (array, item) => {
4-
// Remove item from array modifying original array
4+
return array.filter(function (x) {
5+
return x != item;
6+
});
57
};
68

79
module.exports = { removeElement };

Exercises/2-elements.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
'use strict';
1+
"use strict";
22

33
const removeElements = (array, ...items) => {
4-
// Remove multiple items from array modifying original array
4+
for (const val of items) {
5+
const index = array.indexOf(val);
6+
if (index !== -1) array.splice(index, 1);
7+
}
8+
return array;
59
};
610

711
module.exports = { removeElements };

Exercises/3-unique.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
'use strict';
1+
"use strict";
22

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

6-
const unique = (array) => [];
6+
const unique = (array) => {
7+
//let unique = [...new Set(array)];
8+
const newArray = [];
9+
for (const item of array) {
10+
if (!newArray.includes(item)) {
11+
newArray.push(item);
12+
}
13+
}
14+
return newArray;
15+
};
716

817
module.exports = { unique };

Exercises/4-difference.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
'use strict';
1+
"use strict";
22

33
// Find difference of two arrays
44
// elements from array1 that are not includes in array2
55

6-
const difference = (array1, array2) => [];
7-
6+
const difference = (array1, array2) => {
7+
const dif = [];
8+
for (const item of array1) {
9+
if (!array2.includes(item)) {
10+
dif.push(item);
11+
}
12+
}
13+
return dif;
14+
};
815
module.exports = { difference };

0 commit comments

Comments
 (0)