|
1 | 1 | /** |
2 | 2 | |
3 | 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. |
| 6 | + The function should remove duplicate elements. So the result should be: |
| 7 | + ['a', 'b', 'c', 'd', 'e', 'f'] |
| 8 | + */ |
4 | 9 |
|
5 | | -Write a function called removeDuplicates. This function accept an array as an argument |
6 | | -does not return anything but removes any duplicate elements from the array. |
7 | | -
|
8 | | - The function should remove duplicate elements.So the result should be: |
9 | | -
|
10 | | - |
| 10 | +/** |
| 11 | + * Checks your solution against correct solution |
| 12 | + * @param {Array} array your solution |
| 13 | + * @returns boolean |
11 | 14 | */ |
| 15 | +function checkSolution(array) { |
| 16 | + const solution = ['a', 'b', 'c', 'd', 'e', 'f']; |
| 17 | + if (array == null) return false; |
| 18 | + if (array.length !== solution.length) return false; |
12 | 19 |
|
| 20 | + for (let i = 0; i < solution.length; i++) { |
| 21 | + if (array[i] !== solution[i]) return false; |
| 22 | + } |
| 23 | + return true; |
| 24 | +} |
13 | 25 |
|
14 | 26 | // WRITE YOUR FUNCTION HERE |
| 27 | +function removeDuplicates(array){ |
| 28 | + const newArray = array.filter((item,index) => array.indexOf(item)=== index); |
| 29 | +console.log(newArray); |
| 30 | +}; |
15 | 31 |
|
16 | 32 | const letters = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c', 'b']; |
17 | | - |
18 | 33 | removeDuplicates(letters); |
19 | 34 |
|
20 | | -if (letters === ['a', 'b', 'c', 'd', 'e', 'f']) |
21 | | - console.log("Hooray!") |
| 35 | +if (checkSolution(letters)) { |
| 36 | + console.log("Hooray!"); |
| 37 | +} |
0 commit comments