Skip to content

Commit 5055ce0

Browse files
committed
Writes function that removes duplicates from an array
1 parent 30e6311 commit 5055ce0

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

Week3/homework/step3-bonus.js

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

3-
const values = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c'];
4-
53
function makeUnique(arr) {
6-
// Replace this comment and the next line with your code
7-
console.log(arr);
4+
let newArr = [];
5+
for (let i = 0; i < arr.length; i++){
6+
if (newArr.indexOf(arr[i]) === -1) {
7+
newArr.push(arr[i]);
8+
}
9+
}
10+
return newArr;
811
}
912

13+
const values = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c'];
14+
const names = ['lisa', 'lisa', 'priscilla', 'tania', 'priscilla', 'nadia', 'nadia', 'nadia'];
15+
const numbers = [22, 22, 34, 30, 30, 30, 22, 15, 12, 29, 29, 28, 29];
16+
17+
1018
const uniqueValues = makeUnique(values);
11-
console.log(uniqueValues);
19+
const uniqueNames = makeUnique(names);
20+
const uniqueNumbers = makeUnique(numbers);
21+
22+
console.log(uniqueValues, uniqueNames, uniqueNumbers);
23+
24+
25+
// Another SHORTER option is:
26+
// let newArr = [... new Set(arr)];
27+
// return newArr;
1228

1329
// Do not change or remove anything below this line
14-
module.exports = makeUnique;
30+
module.exports = makeUnique;

0 commit comments

Comments
 (0)