cutTheSticks

    0

    0

    This code is a function that takes in an array of objects and cuts the array of objects at a specific position. The function first sorts the array of objects and then cuts the array at the position specified by the second parameter. The function then returns the trimmed array.

    function cutTheSticks(arr) {
      let result = [];
    
      while (arr.length > 0) {
        result.push(arr.length);
    
        arr.sort((a, b) => a - b);
    
        let front = arr[0];
    
        arr.reduce((target, value, index) => {
          arr[index] -= front;
    
          return target;
        }, []);
    
        let remove = arr.lastIndexOf(0) + 1;
    
        arr.splice(0, remove);
      }
    
      return result;
    }
    
    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.