sockMerchant

    0

    0

    The sockMerchant function accepts two parameters, n, which is the number of connections, and ar, an Array of Objects. The function creates an Array of Objects and converts it to a sortable array. The sockMercher then uses the slice() method to get the first two elements of the Array, and logs them to the console. Next, it checks to see if the first two elements of the Array are equal and, if so, increments the count variable. After the for loop completes, the sockMerchant returns the count variable.

    function sockMerchant(n, ar) {
      let sortArr = ar.sort((a, b) => a - b);
      let count = 0;
      for (let i = 0; i < n; i++) {
        let a = sortArr.slice(i, i + 2);
        console.log(a);
        if (a[0] === a[1]) {
          count++;
          i++;
        }
      }
      return count;
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.