organizingContainers.js

    0

    0

    This code is attempting to organize a container of objects by row and column. The function first creates an empty array of objects and reduces it using the target, items, and index parameters. The target array will have an entry for each object in the items array and each entry will have the row and column of the object it represents. The code then checks to see if the objects in the row and column arrays are the same. If they aren't, the code breaks and returns the result.

    // Complete the organizingContainers function below.
    function organizingContainers(container) {
      let result = true;
    
      let { row, col } = container.reduce(
        (target, items, index) => {
          items.reduce((itemTarget, item, subIndex) => {
            target["row"][index] += item;
            target["col"][subIndex] += item;
    
            return itemTarget;
          }, []);
    
          return target;
        },
        {
          row: new Array(container.length).fill(0),
          col: new Array(container.length).fill(0)
        }
      );
    
      row.sort();
      col.sort();
    
      for (let value of Array.from(
        { length: container.length },
        (value, index) => index
      )) {
        if (row[value] !== col[value]) {
          result = false;
          break;
        }
      }
    
      return result ? "Possible" : "Impossible";
    }
    
    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.