Advertisement
makispaiktis

Codecademy - Recreating the Lodash Libraby (JS File)

Dec 25th, 2019 (edited)
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const _ = {
  2.  
  3.     // METHOD 1 ----> .clamp()
  4.     clamp(number, lower, upper){
  5.       if(lower >= upper){
  6.         console.log("ERROR! " + lower + " is NOT lower than " + upper);
  7.       }
  8.       else{
  9.         if(number <= lower){
  10.           return lower;
  11.         }
  12.         else if(number >= upper){
  13.           return upper;
  14.         }
  15.         else{
  16.           return number;
  17.         }
  18.       }
  19.     },
  20.     // METHOD 2 ----> .inRange()
  21.     inRange(number, start, end){
  22.       // 1st step: Check if end is undefined
  23.       // If this is true, end = start and start = 0
  24.       if(end === undefined){
  25.         end = start;
  26.         start = 0;
  27.       }
  28.       // 2nd step: Check if start > end (we don't want this to be true)
  29.       // If this is true, I will swap the values
  30.       if(start > end){
  31.         let temp = start;
  32.         start = end;
  33.         end = temp;
  34.       }
  35.       // 3rd step: Solution
  36.       if(number < start || number >= end){
  37.         return false;
  38.       }
  39.       else{
  40.         return true;
  41.       }
  42.     },
  43.     // METHOD 3 ----> .words()
  44.     words(story){
  45.       /*
  46.       // First, I will find the 'spacebar characters', in order to split the words
  47.       let spacebarIndex = [];  
  48.       spacebarIndex.push(0);  // 0 means to begin from the 1st position
  49.       for(let i=0; i<story.length; i++){
  50.         if(story[i] === ' '){
  51.           spacebarIndex.push(i);
  52.         }
  53.       }
  54.       spacebarIndex.push(story.length);  // This means where the final word ends
  55.       // Now, the array of 'spacebar characters' has all these indexes
  56.       let result = [];
  57.       for(let i=0; i<spacebarIndex.length-1; i++){
  58.         str = "";
  59.         for(let j=spacebarIndex[i]; j<spacebarIndex[i+1]; j++){
  60.           str += story[j];
  61.         }
  62.         result.push(str);
  63.       }
  64.       return result;
  65.       */
  66.       return story.split(' ');
  67.     },
  68.     // METHOD 4 ----> .pad()
  69.     pad(str, desiredLength){
  70.       if(str.length >= desiredLength){
  71.         return str;
  72.       }
  73.       else{
  74.         let newStr = "";
  75.         let padding = desiredLength - str.length;
  76.         let halfPadding = padding / 2;
  77.         if(halfPadding === Math.floor(halfPadding)){
  78.           for(let i=0; i<halfPadding; i++){
  79.             newStr += " ";
  80.           }
  81.           newStr += str;
  82.           for(let i=0; i<halfPadding; i++){
  83.             newStr += " ";
  84.           }
  85.         }
  86.         else{
  87.           let leftPadding = Math.floor(halfPadding);
  88.           let rightPadding = leftPadding + 1;
  89.           for(let i=0; i<leftPadding; i++){
  90.             newStr += " ";
  91.           }
  92.           newStr += str;
  93.           for(let i=0; i<rightPadding; i++){
  94.             newStr += " ";
  95.           }
  96.         }
  97.         return newStr;
  98.       }  // END OF ELSE-CASE (desiredLength > length)
  99.     },
  100.     // METHOD 5 ----> .has()
  101.     has(obj, key){
  102.       // Key (the parameter) will be sth like string
  103.       // 2nd param = input = 'name' for example
  104.       // To access the value , I MUST DO:
  105.       // obj[key], and now I can have access
  106.       if(obj[key] != undefined){
  107.         return true;
  108.       }
  109.       else{
  110.         return false;
  111.       }
  112.     },
  113.     // METHOD 6 ----> .invert()
  114.     invert(obj){
  115.       let newObj = {};
  116.       for(let [key, value] of Object.entries(obj)){
  117.         newObj[value] = key;
  118.       }
  119.       return newObj;
  120.     },
  121.     // METHOD 7 ----> .findKey()
  122.     findKey(obj, predicate){
  123.       for(let key in obj){
  124.         let value = obj[key];
  125.         let predicateReturnValue = predicate(value);
  126.         if(predicateReturnValue){
  127.           return key;
  128.         }  
  129.       }
  130.       return undefined;
  131.     },
  132.     // METHOD 8 ----> .drop()
  133.     drop(arr, num){
  134.       let newArr = arr.slice();
  135.       if(num === undefined){
  136.         newArr.shift();
  137.         return newArr;
  138.       }
  139.       else{
  140.         for(let i=0; i<num; i++){
  141.           newArr.shift();
  142.         }
  143.         return newArr;
  144.       }
  145.     },
  146.     // METHOD 9 ----> .dropWhile()
  147.     dropWhile(arr, predicate){
  148.       let index = arr.findIndex((element, index) => {
  149.         return (!predicate(element, index,  arr));
  150.       });
  151.       // I have the index, where this proposal is falsy
  152.       // If index = 4, I will drop 4 elements (0,1,2,3)
  153.       let result = this.drop(arr, index);
  154.       return result;
  155.     },
  156.     // METHOD 10 ----> .chunk()
  157.     chunk(arr, size){
  158.       if(size === undefined){
  159.         size = 1;
  160.       }
  161.       if(size >= arr.length){
  162.         return arr;
  163.       }
  164.       // Begin to chunk (size = step in for-loop)
  165.       let newArr = [];
  166.       for(let i=0; i<arr.length; i+=size){
  167.         let tempArr = [];
  168.         for(let j=i; j<i+size; j++){
  169.           if(j < arr.length){
  170.             // This condition must be true, in order to avoid overflow of array indexes
  171.             tempArr.push(arr[j]);
  172.           }
  173.           else{
  174.             break;  // In other case, I have infinite loop
  175.           }
  176.         }  // END OF SECOND FOR-LOOP
  177.         newArr.push(tempArr);
  178.       }  // END OF FIRST FOR-LOOP
  179.       return newArr;
  180.     }
  181.    
  182.   };  // END OF OBJECT
  183.  
  184.  
  185.  
  186.  
  187.   // Do not write or modify code below this line.
  188.   module.exports = _;
  189.   // 1. Method .clamp()
  190.   console.log();
  191.   console.log("Method 1 ----> .clamp()");
  192.   console.log("_.clamp(10, 11, 15) = " + _.clamp(10, 11, 15));
  193.   console.log("_.clamp(16, 11, 15) = " + _.clamp(16, 11, 15));
  194.   console.log("_.clamp(12, 11, 15) = " + _.clamp(12, 11, 15));
  195.  
  196.   // 2. Method .inRange()
  197.   console.log();
  198.   console.log("Method 2 ----> .inRange()");
  199.   console.log("_.inRange(3, 1, 20) = " + _.inRange(3, 1, 20));
  200.   console.log("_.inRange(1, 1, 20) = " + _.inRange(1, 1, 20));
  201.   console.log("_.inRange(20, 1, 20) = " + _.inRange(20, 1, 20));
  202.   console.log("_.inRange(3, 20, 1) = " + _.inRange(3, 20, 1));
  203.   console.log("_.inRange(3, 1) = " + _.inRange(3, 1));
  204.   console.log("_.inRange(3, 20) = " + _.inRange(3, 20));
  205.  
  206.   // 3. Method .words()
  207.   console.log();
  208.   console.log("Method 3 ----> .words()");
  209.   console.log("_.words('Hello my friends! How was your day?') = ");
  210.   console.log(_.words('Hello my friends! How was your day?'));
  211.  
  212.   // 4. Method .pad()
  213.   console.log();
  214.   console.log("Method 4 ----> .pad()");
  215.   console.log("_.pad('hi', 6) becomes: ");
  216.   console.log(_.pad('hi', 6));
  217.   console.log("_.pad('hi', 5) becomes: ");
  218.   console.log(_.pad('hi', 5));
  219.   console.log("_.pad('hi', 1) becomes: ");
  220.   console.log(_.pad('hi', 1));
  221.   console.log("_.pad('hi', 2) becomes: ");
  222.   console.log(_.pad('hi', 2));
  223.  
  224.   // 5. Method .has()
  225.   console.log();
  226.   console.log("Method 5 ----> .has()");
  227.   console.log("~~~~~~~~~~ Example 1 ~~~~~~~~~~");
  228.   console.log("_.has({");
  229.   console.log("        id1: 5,");
  230.   console.log("        id2: 1,");
  231.   console.log("        phrase: 'think'");
  232.   console.log("     }, 'name') = ");
  233.   console.log(_.has({id1: 5, id2: 1, phrase: 'think'}, 'name'));
  234.   console.log("~~~~~~~~~~ Example 2 ~~~~~~~~~~");
  235.   console.log("_.has({");
  236.   console.log("        name: 'Thomas',");
  237.   console.log("        age: undefined,");
  238.   console.log("        myKey: 2");
  239.   console.log("     }, 'name') = ");
  240.   console.log(_.has({name: 'Thomas', age: 20, myKey: undefined}, 'name'));
  241.   console.log("~~~~~~~~~~ Example 3 ~~~~~~~~~~");
  242.   console.log("_.has({");
  243.   console.log("        name: 'Thomas',");
  244.   console.log("        age: 20,");
  245.   console.log("        something: 'undefined'");
  246.   console.log("     }, 'name') = ");
  247.   console.log(_.has({name: 'Thomas', age: 20, something: 'undefined'}, 'name'));
  248.   console.log("~~~~~~~~~~ Examples end ~~~~~~~~~~");
  249.  
  250.   // 6. Method .invert()
  251.   console.log();
  252.   console.log("Method 6 ----> .invert()");
  253.   console.log("_.invert({name:'Tom', surname:'Bouf'}) will be: ");
  254.   console.log(_.invert({name:'Tom', surname:'Bouf'}));
  255.  
  256.   // 8. Method .drop()
  257.   console.log();
  258.   console.log("Method 8 ----> .drop()");
  259.   console.log("_.drop([1,2,3,4,5,6,7,8,9,10], 3) = " + _.drop([1,2,3,4,5,6,7,8,9,10], 3));
  260.   console.log("_.drop([1,2,3,4,5,6,7,8,9,10]) = " + _.drop([1,2,3,4,5,6,7,8,9,10]));
  261.  
  262.   // 10. Method .chunk()
  263.   console.log();
  264.   console.log("Method 10 ----> .chunk()");
  265.   console.log("_.chunk([1,2,3,4,5,6,7,8,9,10], 2) = ")
  266.   console.log(_.chunk([1,2,3,4,5,6,7,8,9,10], 2));
  267.   console.log();
  268.   console.log("_.chunk([1,2,3,4,5,6,7,8,9,10], 3) = ");
  269.   console.log(_.chunk([1,2,3,4,5,6,7,8,9,10], 3));
  270.   console.log();
  271.   console.log("_.chunk([1,2,3,4,5,6,7,8,9,10]) = ");
  272.   console.log(_.chunk([1,2,3,4,5,6,7,8,9,10]));
  273.   console.log();
  274.   console.log("_.chunk([1,2,3,4,5,6,7,8,9,10], 20) = ");
  275.   console.log(_.chunk([1,2,3,4,5,6,7,8,9,10], 20));
  276.   console.log();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement