22
33const items = [ 'Pencil' , 'Notebook' , 'yo-yo' , 'Gum' ] ;
44
5- /*
5+
66
77 //Given this problem:
88
@@ -13,6 +13,7 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
1313 // Potential Solution:
1414
1515 // Higher order function using "cb" as the call back
16+
1617 function firstItem ( arr , cb ) {
1718 return cb ( arr [ 0 ] ) ;
1819 }
@@ -22,16 +23,39 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2223 console . log ( first )
2324 } ) ;
2425
25- * /
26+ / /
2627
28+ function getLength ( arr , cb ) {
29+ // getLength passes the length of the array into the callback.
30+ }
2731
28- function getLength ( arr , cb ) {
29- // getLength passes the length of the array into the callback.
30- }
32+ function getLength ( arr , cb ) {
33+ return cb ( arr . length ) ;
34+ }
3135
32- function last ( arr , cb ) {
33- // last passes the last item of the array into the callback.
34- }
36+ getLength ( items , function ( length ) {
37+ console . log ( length )
38+ } ) ;
39+
40+
41+
42+ //
43+
44+ function last ( arr , cb ) {
45+ // last passes the last item of the array into the callback.
46+ }
47+
48+ function last ( arr , cb ) {
49+ return cb ( arr [ arr . length - 1 ] ) ;
50+ }
51+
52+
53+ last ( items , function ( anything ) {
54+ console . log ( anything )
55+ } ) ;
56+
57+
58+ //
3559
3660function sumNums ( x , y , cb ) {
3761 // sumNums adds two numbers (x, y) and passes the result to the callback.
0 commit comments