|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -const values = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c']; |
| 3 | +// Add your code here |
| 4 | +// numberValues stringValues |
| 5 | +const stringValues = ['a', 'b', 'a', 'c','a', 'd', 'a', 'e','e','e','e','e','e','e','e','e', 'f', 'c' , 'f' , 'f' , 'f' ]; |
4 | 6 |
|
5 | | -// Add your function here. Try and come up with a good name for this function |
| 7 | +const numberValues = [1 , 2, 3, 4, 1, 6, 5 ,4,4,4,4,4,2,2,2,2,2,2,2,2,2,2, 0 , 6 ,6 , 6 , 6 , 6]; |
6 | 8 |
|
7 | | -// Replace `yourFunction` with the name of the function you just created |
8 | | -const uniqueValues = yourFunction(values); |
| 9 | +function sortUniquerNumberArryAndStringArry( arr ) { |
9 | 10 |
|
10 | | -console.log(uniqueValues); |
| 11 | + if ( typeof arr[0] == "string" ) { |
| 12 | + arr.sort(); |
| 13 | + } |
| 14 | + |
| 15 | + else if ( typeof arr[0] == "number" ) { |
| 16 | + arr.sort(function(a, b){return a - b}); |
| 17 | + } |
| 18 | + |
| 19 | + else { |
| 20 | + return console.error('The array is not a "string" or a "number" '); |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + for ( var i = 0 ; i < arr.length -1 ; i ++) { |
| 26 | + |
| 27 | + if ( typeof arr[ i ] === typeof arr[ i + 1] ){ |
| 28 | + |
| 29 | + if ( arr[ i ] === arr[ i + 1] ){ |
| 30 | + |
| 31 | + arr.splice(i, 1); |
| 32 | + |
| 33 | + sortUniquerNumberArryAndStringArry( arr ); |
| 34 | + |
| 35 | + } |
| 36 | + } |
| 37 | + else{ |
| 38 | + return ; // The array contains different types of values so it will return undefined |
| 39 | + } |
| 40 | + |
| 41 | + } |
| 42 | + return arr; |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +const uniqueStringValues = sortUniquerNumberArryAndStringArry( stringValues ); |
| 47 | +console.log(uniqueStringValues); |
| 48 | + |
| 49 | + |
| 50 | +const uniqueNumberValues = sortUniquerNumberArryAndStringArry( numberValues ); |
| 51 | +console.log(uniqueNumberValues); |
0 commit comments