Skip to content

Commit f17e72b

Browse files
committed
Updated JS2 Week3 example2 to correctly check for correct solution
1 parent b6e4913 commit f17e72b

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed
Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
/**
22
33
** 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.
46
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+
The function should remove duplicate elements. So the result should be:
8+
['a', 'b', 'c', 'd', 'e', 'f']
79
8-
The function should remove duplicate elements.So the result should be:
10+
*/
911

10-
12+
/**
13+
* Checks your solution against correct solution
14+
* @param {Array} array your solution
15+
* @returns boolean
1116
*/
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;
1221

22+
for (let i = 0; i < solution.length; i++) {
23+
if (array[i] !== solution[i]) return false;
24+
}
25+
return true;
26+
}
1327

1428
// WRITE YOUR FUNCTION HERE
1529

1630
const letters = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c', 'b'];
17-
1831
removeDuplicates(letters);
1932

20-
if (letters === ['a', 'b', 'c', 'd', 'e', 'f'])
21-
console.log("Hooray!")
33+
if (checkSolution(letters)) {
34+
console.log("Hooray!");
35+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy