-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm3.html
More file actions
36 lines (27 loc) · 957 Bytes
/
algorithm3.html
File metadata and controls
36 lines (27 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!DOCTYPE html>
<html>
<body>
<script>
//https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/seek-and-destroy
//You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.
/* Solution #1 with for cycle
function destroyer(arr) {
for (var i=1; i<arguments.length; i++) {
if (arr.indexOf(arguments[i]) > 0) {
arr.splice(arr.indexOf(arguments[i]),1)
}
}
return arr;
}
*/
function destroyer(arr) {
var newArgs = Array.from(arguments).slice(1);
return arr.filter(x=> (newArgs.indexOf(x) < 0));
}
//tests
console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));
console.log(destroyer(["tree", "hamburger", 53], "tree", 53));
console.log(destroyer([2, 3, 2, 3], 2, 3));
</script>
</body>
</html>