Skip to content

Commit 6826f59

Browse files
committed
Ex2
1 parent 731c47d commit 6826f59

1 file changed

Lines changed: 16 additions & 32 deletions

File tree

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,19 @@
1-
/**
2-
3-
** Exercise 2: The lottery machine **
4-
Write a function called removeDuplicates. This function accept an array as an argument
5-
does not return anything but removes any duplicate elements from the array.
1+
const letters = ['a', 'b', 'b', 'c', 'd', 'a', 'e', 'f', 'f', 'c', 'b'];
2+
const nonDuplicates = [];
63

7-
The function should remove duplicate elements. So the result should be:
8-
['a', 'b', 'c', 'd', 'e', 'f']
9-
10-
*/
11-
12-
/**
13-
* Checks your solution against correct solution
14-
* @param {Array} array your solution
15-
* @returns boolean
16-
*/
17-
function checkSolution(array) {
18-
const solution = ['a', 'b', 'c', 'd', 'e', 'f'];
19-
if (array == null) return false;
20-
if (array.length !== solution.length) return false;
21-
22-
for (let i = 0; i < solution.length; i++) {
23-
if (array[i] !== solution[i]) return false;
4+
function removeDuplicates(array) {
5+
nonDuplicates.push(array[0]);
6+
for (let i = 1; i < array.length; i++) {
7+
const x = [];
8+
for (let j = 0; j < i; j++) {
9+
if (array[j] == array[i]) {
10+
x.push(array[j]);
11+
}
12+
}
13+
if (x.length == 0) {
14+
nonDuplicates.push(array[i]);
15+
}
2416
}
25-
return true;
17+
return nonDuplicates;
2618
}
27-
28-
// WRITE YOUR FUNCTION HERE
29-
30-
const letters = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c', 'b'];
31-
removeDuplicates(letters);
32-
33-
if (checkSolution(letters)) {
34-
console.log("Hooray!");
35-
}
19+
console.log(removeDuplicates(letters));

0 commit comments

Comments
 (0)