[Demo] CheckBox Click When Disabled

[Demo] CheckBox Click When Disabled

by dying_sakura

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div onclick="showAlert();">
    <input id="diff" name="diff" type="number" value="100"/>
</div>

JavaScript

var difff = parseInt(document.getElementById('diff').value);

const source = [
  {
    entryID: 1,
    entryName: "player1",
    weight: 2200,
  },
  {
    entryID: 2,
    entryName: "player2",
    weight: 1920,
  },  {
    entryID: 3,
    entryName: "player3",
    weight: 2020,
  },

];

function combine(
  data = [],
  different = difff,
  maxGroupSize = 2,
  sortedStatement = (a, b) => a.weight - b.weight
) {
  const sortedData = [...data].sort(sortedStatement); // "weight" must be in ascending order

  const dataGroups = sortedData.reduce((acc, item) => {
    const findedAccItem = acc.find(
      (accItem) =>
        accItem.length < maxGroupSize && // if the array is not filled
        accItem[0].weight + different >= item.weight && // and if the minimum is in the acceptable range
        !accItem.find((obj) => obj.entryName === item.entryName /* || obj.entryID === item.entryID */) // and if there are no matches
    );
    if (findedAccItem) {
      findedAccItem.push(item);
    } else {
      acc.push([item]);
    }
    return acc;
  }, []);

  const namedDataGroups = dataGroups.reduce((acc, item, index) => {
    // added an index as a prefix because the keys can match
    const key = [index, ...item.map((item) => item.weight)].join("_");
    acc[key] = item;
    return acc;
  }, {});

  return namedDataGroups;
}

// default example
console.log("Example #1: ", combine(source));

console.log(difff);
// advanced example: displaying elements in reverse order if the weight properties are equal