Advertisement
padznich

Untitled

Oct 18th, 2017
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function customFilter(array, callback) {
  3.     var result = [];
  4.         for (var i = 0; i < array.length; i++) {
  5.             if (callback(array[i])) {
  6.                 result.push(array[i]);
  7.             }
  8.         }
  9.     return result;
  10. }
  11.  
  12.  
  13.  
  14. function check_filter(origin_array, examine_filter, arrow_function) {
  15.     // Get filtered arrays
  16.     var filtered_origin_array = origin_array.filter(arrow_function);
  17.     var filtered_examine_array = customFilter(origin_array, arrow_function);
  18.     // Check for correct filtering. Return "true" in case of correct filter, "false" - otherwise
  19.     if (Math.min.apply(Math, filtered_examine_array) == Math.min.apply(Math, filtered_origin_array) &&
  20.         Math.max.apply(Math, filtered_examine_array) == Math.max.apply(Math, filtered_origin_array) &&
  21.         filtered_examine_array.length == filtered_origin_array.length) {
  22.        
  23.         return true
  24.     }
  25.  
  26. }
  27.  
  28. // Test cases
  29. // Case 1
  30. var arr_1 = [-5, -2, 0, 1, 2, 5, 9];
  31. fn_1 = item => item > 1;
  32. console.log(check_filter(arr_1, customFilter, fn_1), arr_1);
  33.  
  34. // Case 2
  35. var arr_2 = [0, 3, 4, 7, 12];
  36. fn_2 = item => item < 5;
  37. console.log(check_filter(arr_2, customFilter, fn_2), arr_2);
  38.  
  39. // DEBUG
  40. console.log(Math.max.apply(Math, [1,2,3,4,5]));
  41. console.log(Math.min.apply(Math, arr_1));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement