11'use strict' ;
22
33const values = [ 'a' , 'b' , 'c' , 'd' , 'a' , 'e' , 'f' , 'c' ] ;
4+ const numberArray = [ 1 , 1 , 2 , 5 , 5 , 5 , 6 , 3 , 9 ] ;
5+ const mixedArray = [ 1 , true , "coffee" , { 2 : 1 , 4 : 2 } , false , true , "water" ] ;
46
5- // Add your function here. Try and come up with a good name for this function
7+ function smartSorting ( anyArray ) {
68
7- // Replace `yourFunction` with the name of the function you just created
8- const uniqueValues = yourFunction ( values ) ;
9+ anyArray . sort ( ) ;
10+ let sortedArray = [ ] ;
911
10- console . log ( uniqueValues ) ;
12+ for ( let i = anyArray . length - 1 ; i >= 0 ; i -- ) {
13+ if ( anyArray [ i ] != anyArray [ i - 1 ] ) {
14+ sortedArray . push ( anyArray [ i ] ) ;
15+ } ;
16+ if ( typeof anyArray [ i ] != "string" && typeof anyArray [ i ] != "number" ) {
17+ return "Sorry, I have to stop. I only want numbers or strings. Bye!" ;
18+ }
19+
20+ }
21+ return sortedArray . sort ( ) ;
22+ }
23+
24+ const uniqueValues = smartSorting ( values ) ;
25+ const uniqueNumbers = smartSorting ( numberArray ) ;
26+ const mixedTest = smartSorting ( mixedArray ) ;
27+
28+
29+ console . log ( "Array from assignment: " + values ) ;
30+ console . log ( "Sorted version: " + uniqueValues ) ;
31+
32+ console . log ( "Array with numbers: " + numberArray ) ;
33+ console . log ( "Sorted version: " + uniqueNumbers ) ;
34+
35+ console . log ( "Mixed array with different data types: " + mixedArray ) ;
36+ console . log ( "It doesn't accept any non-number & string values:\n" + mixedTest ) ;
0 commit comments