Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var _; // globals
  2.  
  3. describe("About Applying What We Have Learnt", function() {
  4.   var products;
  5.  
  6.   beforeEach(function () {
  7.     products = [
  8.        { name: "Sonoma", ingredients: ["artichoke", "sundried tomatoes", "mushrooms"], containsNuts: false },
  9.        { name: "Pizza Primavera", ingredients: ["roma", "sundried tomatoes", "goats cheese", "rosemary"], containsNuts: false },
  10.        { name: "South Of The Border", ingredients: ["black beans", "jalapenos", "mushrooms"], containsNuts: false },
  11.        { name: "Blue Moon", ingredients: ["blue cheese", "garlic", "walnuts"], containsNuts: true },
  12.        { name: "Taste Of Athens", ingredients: ["spinach", "kalamata olives", "sesame seeds"], containsNuts: true }
  13.     ];
  14.   });
  15.  
  16.   /*********************************************************************************/
  17.  
  18.   it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (imperative)", function () {
  19.     var i,j,hasMushrooms, productsICanEat = [];
  20.  
  21.     for (i = 0; i < products.length; i+=1) {
  22.         if (products[i].containsNuts === false) {
  23.             hasMushrooms = false;
  24.             for (j = 0; j < products[i].ingredients.length; j+=1) {
  25.                if (products[i].ingredients[j] === "mushrooms") {
  26.                   hasMushrooms = true;
  27.                }
  28.             }
  29.             if (!hasMushrooms) productsICanEat.push(products[i]);
  30.         }
  31.     }
  32.  
  33.     expect(productsICanEat.length).toBe(1);
  34.   });
  35.  
  36.   it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (functional)", function () {
  37.       var productsICanEat = [];
  38.  
  39.  
  40.       productsICanEat = products.filter(function (product){
  41.           return !product.containsNuts && product.ingredients.every(function (value){
  42.             return value !== 'mushrooms';
  43.           })
  44.       })
  45.  
  46.  
  47.       /* solve using filter() & all() / any() */
  48.  
  49.       expect(productsICanEat.length).toBe(1);
  50.   });
  51.  
  52.   /*********************************************************************************/
  53.  
  54.   it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (imperative)", function () {
  55.     var sum = 0;
  56.  
  57.     for(var i=1; i<1000; i+=1) {
  58.       if (i % 3 === 0 || i % 5 === 0) {
  59.         sum += i;
  60.       }
  61.     }
  62.    
  63.     expect(sum).toBe(233168);
  64.   });
  65.  
  66.   it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)", function () {
  67.     var sum =
  68.     _.range(1000).reduce(function(start, value){
  69.         return (value % 3 === 0 || value % 5 === 0) ? start + value: start;
  70.     });    /* try chaining range() and reduce() */
  71.  
  72.  
  73.  
  74.     expect(233168).toBe(sum);
  75.   });
  76.  
  77.   /*********************************************************************************/
  78.    it("should count the ingredient occurrence (imperative)", function () {
  79.     var ingredientCount = { "{ingredient name}": 0 };
  80.  
  81.     for (i = 0; i < products.length; i+=1) {
  82.         for (j = 0; j < products[i].ingredients.length; j+=1) {
  83.             ingredientCount[products[i].ingredients[j]] = (ingredientCount[products[i].ingredients[j]] || 0) + 1;
  84.         }
  85.     }
  86.  
  87.     expect(ingredientCount['mushrooms']).toBe(2);
  88.   });
  89.  
  90.   it("should count the ingredient occurrence (functional)", function () {
  91.     var ingredientCount = { "{ingredient name}": 0 };
  92.  
  93.       var count = 0;
  94.     _.flatten(products.map(function(product){
  95.       return product.ingredients.map(function(ingredient){
  96.           return ingredient == "mushrooms" ? 1 : 0;
  97.       });
  98.     })).reduce(function(sum, value){
  99.       return sum + value;});
  100.     });
  101.  
  102.     /* chain() together map(), flatten() and reduce() */
  103.  
  104.     expect(ingredientCount['mushrooms']).toBe(2);
  105.   });
  106.  
  107.   /*********************************************************************************/
  108.   /* UNCOMMENT FOR EXTRA CREDIT */
  109.   /*
  110.   it("should find the largest prime factor of a composite number", function () {
  111.  
  112.   });
  113.  
  114.   it("should find the largest palindrome made from the product of two 3 digit numbers", function () {
  115.    
  116.   });
  117.  
  118.   it("should find the smallest number divisible by each of the numbers 1 to 20", function () {
  119.      
  120.    
  121.   });
  122.  
  123.   it("should find the difference between the sum of the squares and the square of the sums", function () {
  124.    
  125.   });
  126.  
  127.   it("should find the 10001st prime", function () {
  128.  
  129.   });
  130.   */
  131. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement