1
- var dojox ; //globals
2
- var df = dojox . lang . functional ;
1
+ var _ ; //globals
3
2
4
3
describe ( "About Applying What We Have Learnt" , function ( ) {
5
4
6
- var operations ;
5
+ var products ;
7
6
8
7
beforeEach ( function ( ) {
9
- operations = [
10
- { direction : "RT" , distance : 200 } ,
11
- { direction : "FWD" , distance : 50 } ,
12
- { direction : "RT" , distance : 100 } ,
13
- { direction : "RT" , distance : 20 } ,
14
- { direction : "FWD" , distance : 200 } ,
15
- { direction : "RT" , distance : 10 }
16
- ]
8
+ products = [
9
+ { name : "Sonoma" , ingredients : [ "artichoke" , "sundried tomatoes" , "mushrooms" ] , containsNuts : false } ,
10
+ { name : "Pizza Primavera" , ingredients : [ "roma" , "sundried tomatoes" , "goats cheese" , "rosemary" ] , containsNuts : false } ,
11
+ { name : "South Of The Border" , ingredients : [ "black beans" , "jalapenos" , "mushrooms" ] , containsNuts : false } ,
12
+ { name : "Blue Moon" , ingredients : [ "blue cheese" , "garlic" , "walnuts" ] , containsNuts : true } ,
13
+ { name : "Taste Of Athens" , ingredients : [ "spinach" , "kalamata olives" , "sesame seeds" ] , containsNuts : true }
14
+ ] ;
17
15
} ) ;
18
16
19
17
/*********************************************************************************/
20
18
21
- it ( "should find a needle in a haystack (imperative)" , function ( ) {
22
-
23
- var findNeedle = function ( ops ) {
24
- var hasInvalidOperation = false ;
25
- for ( var i = 0 ; i < ops . length ; i += 1 ) {
26
- if ( ops [ i ] . direction === "FWD" && ops [ i ] . distance > 100 ) {
27
- hasInvalidOperation = true ;
28
- break ;
29
- }
30
- }
19
+ it ( "given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (imperative)" , function ( ) {
31
20
32
- return hasInvalidOperation ;
33
- } ;
34
-
35
- expect ( findNeedle ( operations ) ) . toBe ( __ ) ;
21
+ var i , j , hasMushrooms , productsICanEat = [ ] ;
22
+
23
+ for ( i = 0 ; i < products . length ; i += 1 ) {
24
+ if ( products [ i ] . containsNuts === false ) {
25
+ hasMushrooms = false ;
26
+ for ( j = 0 ; j < products [ i ] . ingredients . length ; j += 1 ) {
27
+ if ( products [ i ] . ingredients [ j ] === "mushrooms" ) {
28
+ hasMushrooms = true ;
29
+ }
30
+ }
31
+ if ( ! hasMushrooms ) productsICanEat . push ( products [ i ] ) ;
32
+ }
33
+ }
34
+
35
+ expect ( productsICanEat . length ) . toBe ( __ ) ;
36
36
} ) ;
37
37
38
- it ( "should find needle in a haystack (functional)" , function ( ) {
39
- expect ( df . some ( operations , "x.direction === 'FWD' && x.distance > 100" ) ) . toBe ( __ ) ;
38
+ it ( "given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (functional)" , function ( ) {
39
+
40
+ var productsICanEat = [ ] ;
41
+
42
+ /* solve using filter() & all() / any() */
43
+
44
+ expect ( productsICanEat . length ) . toBe ( __ ) ;
40
45
} ) ;
41
46
42
47
/*********************************************************************************/
@@ -54,50 +59,31 @@ describe("About Applying What We Have Learnt", function() {
54
59
} ) ;
55
60
56
61
it ( "should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)" , function ( ) {
57
- var sumIfMultipleOf3Or5 = function ( sum , next ) {
58
- if ( next % 3 === 0 || next % 5 === 0 ) {
59
- return sum + next ;
60
- }
61
- return sum ;
62
- } ;
63
- var numbers = df . repeat ( 1000 , "+1" , 1 ) ;
64
62
65
- expect ( df . reduce ( numbers , sumIfMultipleOf3Or5 , 0 ) ) . toBe ( __ ) ;
63
+ var sum = __ ; /* try chaining range() and reduce() */
64
+
65
+ expect ( 234168 ) . toBe ( __ ) ;
66
66
} ) ;
67
67
68
68
/*********************************************************************************/
69
+ it ( "should count the ingredient occurrence (imperative)" , function ( ) {
70
+ var ingredientCount = { "{ingredient name}" : 0 } ;
69
71
70
- it ( "should find the sum of all the even valued terms in the fibonacci sequence which do not exceed four million (imperative)" , function ( ) {
71
- var sum = 0 ;
72
- var fib = [ 0 , 1 , 1 ] ;
73
- var i = 3 ;
74
- var currentFib = 0 ;
75
-
76
- do {
77
- currentFib = fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ;
78
- if ( currentFib % 2 === 0 ) {
79
- sum += currentFib ;
80
- }
81
- i += 1 ;
82
- } while ( currentFib < 4000000 ) ;
83
-
84
- expect ( sum ) . toBe ( __ ) ;
72
+ for ( i = 0 ; i < products . length ; i += 1 ) {
73
+ for ( j = 0 ; j < products [ i ] . ingredients . length ; j += 1 ) {
74
+ ingredientCount [ products [ i ] . ingredients [ j ] ] = ( ingredientCount [ products [ i ] . ingredients [ j ] ] || 0 ) + 1 ;
75
+ }
76
+ }
77
+
78
+ expect ( ingredientCount [ 'mushrooms' ] ) . toBe ( __ ) ;
85
79
} ) ;
86
80
87
- it ( "should find the sum of all the even valued terms in the fibonacci sequence which do not exceed four million (functional)" , function ( ) {
88
- var calcNextFibTuple = function ( item , index , array ) {
89
- return [ item [ 1 ] , item [ 0 ] + item [ 1 ] ] ;
90
- } ;
91
- var addEven = function ( result , item ) {
92
- if ( item [ 0 ] % 2 === 0 ) {
93
- return result + item [ 0 ] ;
94
- }
95
- return result ;
96
- } ;
97
- var fib = df . until ( "item[0] > 4000000" , calcNextFibTuple , [ 0 , 1 ] ) ;
98
- var sum = df . reduce ( fib , addEven , 0 ) ;
99
-
100
- expect ( sum ) . toBe ( __ ) ;
81
+ it ( "should count the ingredient occurrence (functional)" , function ( ) {
82
+ var ingredientCount = { "{ingredient name}" : 0 } ;
83
+
84
+ /* chain() together map(), flatten() and reduce() */
85
+
86
+ expect ( ingredientCount [ 'mushrooms' ] ) . toBe ( __ ) ;
101
87
} ) ;
102
88
103
89
/*********************************************************************************/
0 commit comments