11const items = [ 'Pencil' , 'Notebook' , 'yo-yo' , 'Gum' , "yo-yo" ] ;
22
3+ //displays callback
4+ const displayCallback = ( arr ) => {
5+ console . log ( arr ) ;
6+ }
7+
38function firstItem ( arr , cb ) {
49 // firstItem passes the first item of the given array to the callback function.
5- return cb ( arr ) ;
6- }
7- const firstIndex = ( arr ) => {
8- console . log ( arr [ 0 ] ) ;
10+ return cb ( arr [ 0 ] ) ;
911}
1012
11- firstItem ( items , firstIndex ) ;
13+ firstItem ( items , displayCallback ) ;
1214
1315function getLength ( arr , cb ) {
1416 // getLength passes the length of the array into the callback.
15- return cb ( arr ) ;
17+ return cb ( arr . length ) ;
1618}
1719
18- const arrayLength = ( arr ) => {
19- console . log ( arr . length ) ;
20- }
21- getLength ( items , arrayLength ) ;
20+ getLength ( items , displayCallback ) ;
2221
2322
2423function last ( arr , cb ) {
2524 // last passes the last item of the array into the callback.
26- return cb ( arr ) ;
27- }
28- const lastIndex = ( arr ) => {
29- console . log ( arr [ arr . length - 1 ] ) ;
25+ return cb ( arr [ arr . length - 1 ] ) ;
3026}
3127
32- last ( items , lastIndex ) ;
28+ last ( items , displayCallback ) ;
3329
3430function sumNums ( x , y , cb ) {
3531 // sumNums adds two numbers (x, y) and passes the result to the callback.
36- return cb ( x , y ) ;
37- }
38- const addNums = ( num1 , num2 ) => {
39- console . log ( num1 + num2 ) ;
32+ return cb ( x + y ) ;
4033}
4134
42- sumNums ( 3 , 4 , addNums ) ;
35+ sumNums ( 3 , 4 , displayCallback ) ;
4336
4437function multiplyNums ( x , y , cb ) {
4538 // multiplyNums multiplies two numbers and passes the result to the callback.
46- return cb ( x , y ) ;
47- }
48-
49- const multiply = ( num1 , num2 ) => {
50- console . log ( num1 * num2 ) ;
39+ return cb ( x * y ) ;
5140}
5241
53- multiplyNums ( 3 , 4 , multiply ) ;
42+ multiplyNums ( 3 , 4 , displayCallback ) ;
5443
5544function contains ( item , list , cb ) {
5645 // contains checks if an item is present inside of the given array/list.
5746 // Pass true to the callback if it is, otherwise pass false.
58- return cb ( item , list ) ;
59- }
60-
61- const checkContains = ( item , list ) => {
6247 if ( list . includes ( item ) ) {
63- console . log ( true ) ;
48+ return cb ( true ) ;
6449 } else {
65- console . log ( false ) ;
50+ return cb ( false ) ;
6651 }
6752}
6853
69- contains ( "Gum" , items , checkContains ) ;
70- contains ( "stick" , items , checkContains ) ;
54+ contains ( "Gum" , items , displayCallback ) ;
55+ contains ( "stick" , items , displayCallback ) ;
7156
7257/* STRETCH PROBLEM */
7358
7459function removeDuplicates ( array , cb ) {
75- // removeDuplicates removes all duplicate values from the given array.
76- // Pass the duplicate free array to the callback function.
77- // Do not mutate the original array.
78- return cb ( array ) ;
79- }
80-
81- const duplicates = ( array ) => {
8260 let newArr = [ ] ;
8361 array . forEach ( ( element ) => {
8462 if ( newArr . includes ( element ) ) {
@@ -87,7 +65,10 @@ const duplicates = (array) => {
8765 newArr . push ( element ) ;
8866 }
8967 } ) ;
90- console . log ( newArr ) ;
68+ // removeDuplicates removes all duplicate values from the given array.
69+ // Pass the duplicate free array to the callback function.
70+ // Do not mutate the original array.
71+ return cb ( array ) ;
9172}
9273
93- removeDuplicates ( items , duplicates ) ;
74+ removeDuplicates ( items , displayCallback ) ;
0 commit comments