Liliana797979

my methods in array

Sep 16th, 2021
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /*
  3. function myMethods(array) {
  4.     let arr = array;
  5.  
  6.     function myIncludes(arr, value) {
  7.         for (const iterator of arr) {
  8.             if (iterator === value) {
  9.                 return true;
  10.             }
  11.         }
  12.         return false;
  13.  
  14.         //Examples: --------------------------------------------------------------
  15.  
  16.         //The includes() returns true if the given value is part of the array
  17.  
  18.         //let myArray = ["Peter", "George", "Mary"];
  19.         //myArray.includes("George"); // true
  20.         //myArray.includes("John"); // false
  21.     }
  22.  
  23.     function myIndexOf(arr, value) {
  24.         for (let index = 0; index < arr.length; index++) {
  25.             if (arr[index] === value) {
  26.                 return index;
  27.             }
  28.         }
  29.         return -1;
  30.  
  31.         //Examples: --------------------------------------------------------------
  32.  
  33.         //The indexOf() returns the index where the given value is stored
  34.         //# Returns -1 if value is not found
  35.  
  36.         //let myArray = ["Peter", "George", "Mary"];
  37.         //myArray.indexOf("Mary"); // 2
  38.         //myArray.indexOf("Nick"); // -1
  39.     }
  40.  
  41.     function mySlice(arr, start, end) {
  42.         let copy = [];
  43.         if (start == undefined) {
  44.             start = 0;
  45.         }
  46.         if (end == undefined) {
  47.             end = arr.length;
  48.         }
  49.         for (let i = start; i < end; i++) {
  50.             copy.push(arr[i]);
  51.         }
  52.         return copy;
  53.  
  54.         //Examples: --------------------------------------------------------------
  55.  
  56.         //The slice() function creates new array from part of another
  57.         //# Note that the original array will not be modified
  58.  
  59.         //let myArray = ["one", "two", "three", "four", "five"];
  60.         //let sliced = myArray.slice(2);
  61.         //console.log(myArray);
  62.         ////["one","two","three","four","five"]
  63.         //console.log(sliced); // ["three","four","five"]
  64.         //console.log(myArray.slice(2, 4)); // ["three","four"]
  65.     }
  66.  
  67.     function mySplice(arr, start, count, elements) {
  68.         let left = arr.slice(0, start);
  69.         let remove = arr.slice(start, start + count);
  70.         let right = arr.slice(start + count);
  71.         arr.length = 0;
  72.         for (const iterator of left) {
  73.             arr.push(iterator);
  74.         }
  75.         if (right != undefined) {
  76.             for (const iterator of elements) {
  77.                 arr.push(iterator);
  78.             }
  79.         }
  80.         for (const iterator of right) {
  81.             arr.push(iterator);
  82.         }
  83.         return remove;
  84.  
  85.         //Examples: --------------------------------------------------------------
  86.  
  87.         //The splice() function adds/removes items to/from an array, and returns the removed item(s)
  88.         //# This function changes the original array
  89.  
  90.         //let nums = [5, 10, 15, 20, 25, 30];
  91.         //let mid = nums.splice(2, 3); // start, delete-count
  92.         //console.log(mid.join('|')); // 15|20|25
  93.         //console.log(nums.join('|')); // 5|10|30
  94.  
  95.         //nums.splice(3, 2, "twenty", "twenty-five");
  96.         //console.log(nums.join('|')); // 5|10|15|twenty|twenty-five|30
  97.     }
  98.  
  99.     function myMap(arr, operator) {
  100.         let result = [];
  101.         for (const iterator of arr) {
  102.             result.push(operator(iterator));
  103.         }
  104.         return result;
  105.  
  106.         //Examples: --------------------------------------------------------------
  107.  
  108.         //The map() function creates new array by applying a function to every element
  109.  
  110.         //let myArr = ['one', 'two', 'three', 'four'];
  111.         //let lengths = myArr.map(x => x.length);
  112.         //console.log(lengths); // [3,3,5,4]
  113.  
  114.         //let numsAsStrings = ["5", "3", "14", "-2", "8"]
  115.         //let nums = numsAsStrings.map(Number);
  116.         //console.log(nums); // [5, 3, 14, -2, 8]
  117.         //let incr = nums.map(x => x + 1);
  118.         //console.log(incr); // [6, 4, 15, -1, 9]
  119.     }
  120.  
  121.     function myFilter(arr, predicate) {
  122.         let result = [];
  123.         for (const iterator of arr) {
  124.             if (predicate(iterator)) {
  125.                 result.push(iterator);
  126.             }
  127.         }
  128.         return result;
  129.  
  130.         //Examples: --------------------------------------------------------------
  131.  
  132.         //The filter() function creates new array from elements matching predicate
  133.         //# Predicate is a function returning a Boolean value (true or false)
  134.  
  135.         //let myArr = ['one', 'two', 'three', 'four'];
  136.         //let longWords = myArr.filter(x => x.length > 3);
  137.         //console.log(longWords); // ['three','four']
  138.  
  139.         //let nums = [5, -11, 3, -2, 8]
  140.         //let positiveNums = nums.filter(x => x > 0);
  141.         //console.log(positiveNums); // [5, 3, 8]
  142.     }
  143.  
  144.     function sorting(arr) {
  145.         arr.sort((a, b) => a - b);
  146.         let newArr = arr.slice(0, 4);
  147.         // console.log(newArr.join(' '));
  148.         // console.log(arr[0], arr[1]);
  149.  
  150.         return newArr.join(' ');
  151.  
  152.         //Examples: --------------------------------------------------------------
  153.  
  154.         //The sort() function sorts the items of an array
  155.         //# Sorting can be alphabetic or numeric, and either ascending (up) or descending (down)
  156.  
  157.         //let names = ["Peter", "George", "Mary"];
  158.         //names.sort(); // Default behaviour – alphabetical order
  159.         //console.log(names); // ["George","Mary","Peter"]
  160.  
  161.         //let numbers = [20, 40, 10, 30, 100, 5];
  162.         //numbers.sort(); // Unexpected result on arrays of numbers!
  163.         //console.log(numbers); // [10,100,20,30,40,5]
  164.  
  165.         //# If result < 0, a is sorted before b , If result < 0, a is sorted before b , If result = 0, a and b are equal (no change)
  166.  
  167.         //let nums = [20, 40, 10, 30, 100, 5];
  168.         //nums.sort((a, b) => a - b); // Compare elements as numbers                   <-- Use that
  169.         //console.log(nums.join('|')); // 5|10|20|30|40|100
  170.  
  171.         //let words = ['nest', 'Eggs', 'bite', 'Grip', 'jAw'];
  172.         //words.sort((a, b) => a.localeCompare(b));                                    <-- Use that
  173.         //// ['bite', 'Eggs', 'Grip', 'jAw', 'nest']
  174.  
  175.         //let wordsLength = ['Isacc', 'Theodor', 'Jack', 'Harrison', 'George'];
  176.         //wordsLength.sort((a, b) => a.length - b.length);                             <-- Use that
  177.         //// ['Jack', 'Isacc', 'George', 'Theodor', 'Harrison']
  178.     }
  179.  
  180.     // console.log(myIncludes(arr, 8));
  181.     // console.log(myIncludes(arr, 10));
  182.     // console.log(myIndexOf(arr, -3));
  183.     // let predic = myFilter(arr, x => x > 7);
  184.     // let nums = myMap(arr, x => x + 1);
  185.     // let result = mySplice(arr, 3, 2, []);
  186.     // let newArr = mySlice(arr, 1, 3);
  187.     // let sorted = sorting(arr);
  188.     // console.log(sorted);
  189.     // console.log(predic);
  190.     // console.log(nums);
  191.     // console.log(result);
  192.     // console.log(newArr);
  193.  
  194. }
  195. myMethods([5, 8, -3, 11, 44, 13, -8]);
  196. */
  197.  
Advertisement
Add Comment
Please, Sign In to add comment