Estoy haciendo un sistema de emparejamiento. Mi función actual emparejó a los jugadores con una diferencia de peso de 20. Como puede ver en el resultado de mi fragmento, 1900 vs 1920 se han emparejado porque declaré un different = 20, . Ahora mi objetivo es hacer una dinámica diferente. Quiero pasar el valor de mi cuadro de texto a mi different . Quiero hacer coincidir el jugador 2 y el jugador 3 . Tienen 30 de diferencia de peso .
Si escribo 30 en mi cuadro de texto. El jugador 2 y el jugador 3 deben coincidir
. ¿Cómo puedo conseguir esto? Cualquier ayuda será apreciada. Gracias.
var difff = document.getElementById('diff'); // I have tried also to set a variable here to get the textbox value and pass it to my "different = difff" but it is not working. const source = [ { entryID: 1, entryName: "player1", weight: 1900, }, { entryID: 2, entryName: "player2", weight: 1920, }, { entryID: 3, entryName: "player3", weight: 1950, }, ]; function combine( data = [], different = 20, // I have tried to change the "20" to my id "difff" So i can pass my textbox's value but it is not working. 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)); <input id="diff" type="number" value="20"/> // I need to pass this on my script.