Estoy trabajando en un programa que debería establecer una respuesta de cadena a un valor específico. He intentado invertir el orden del ciclo, estableciendo variables globales, pero algo está mal en la forma en que se procesan las matrices.
El código de abajo
console.log("The value of the dlArray is") console.log(dlArray) console.log("The value of the elArray is") console.log(elArray) dlArray = shuffle(dlArray); for (let i = numberOfInputs; i < elArray.length+numberOfInputs; i++){ html += '\t\t\t\t\t\t<div id=\'s'; id = (1+i-numberOfInputs); html += id; html +='\' class=\'draggyBox-small ui-draggable\'>\n'; html += '\t\t\t\t\t\t\t' html += elArray[i-numberOfInputs] html += '\n'; html +='\t\t\t\t\t\t</div>\n'; } console.log("The value of the dlArray is") console.log(dlArray) console.log("The value of the elArray is") console.log(elArray) function shuffle(a){ for(let j,i=a.length;i>1;){ j=Math.floor(Math.random()*i--); if (i!=j) [a[i],a[j]]=[a[j],a[i]] } return a }produce la salida real
***************************************** The value of the dlArray is word_match.js:99 (4) ['d1', 'd2', 'd3', 'd4'] word_match.js:100 The value of the elArray is word_match.js:101 (4) ['k1', 'k2', 'k3', 'k4'] word_match.js:113 The value of the dlArray is word_match.js:114 (4) ['d4', 'd3', 'd1', 'd2'] word_match.js:115 The value of the elArray is word_match.js:116 (4) ['k1', 'k2', 'k3', 'k4'] word_match.js:198 k1:d4 k2:d3 k3:d1 k4:d2La salida esperada debe ser
***************************************** The value of the dlArray is word_match.js:99 (4) ['d1', 'd2', 'd3', 'd4'] word_match.js:100 The value of the elArray is word_match.js:101 (4) ['k1', 'k2', 'k3', 'k4'] word_match.js:113 The value of the dlArray is word_match.js:114 (4) ['d4', 'd3', 'd1', 'd2'] word_match.js:115 The value of the elArray is word_match.js:116 (4) ['k1', 'k2', 'k3', 'k4'] word_match.js:198 k1:d1 k2:d2 k3:d3 k4:d4Cualquier ayuda para comprender de dónde proviene el error en este ciclo for sería muy apreciada. Gracias.
Como no está dando muchos detalles, asumiría que dlArray siempre está en orden inverso.
Esta sería otra forma de hacerlo sin un bucle for:
const elArray = ['k1', 'k2', 'k3', 'k4']; const dlArray = ['d4', 'd3', 'd2', 'd1']; dlArray.reverse(); const answers = elArray.map((value, index) => `${value}:${dlArray[index]}`); // Alternative without mutating the original dlArray // // const reversedDl = [...dlArray].reverse(); // const answers = elArray.map((value, index) => `${value}:${reversedDl[index]}` console.log(elArray); console.log(dlArray); console.log(answers.join(' '));ACTUALIZAR Después de sus cambios en la publicación original, entiendo que necesita ordenar el dlArray antes de hacerlo porque podría estar mezclado (?)
Una versión actualizada sería:
const elArray = ['k1', 'k2', 'k3', 'k4']; const dlArray = ['d3', 'd2', 'd1', 'd4']; // Without mutating the original dlArray const sortedDlArray = [...dlArray].sort(); const answers = elArray.map((value, index) => `${value}:${sortedDlArray[index]}`); // Alternative with mutating the original dlArray // dlArray.sort(); // const answers = elArray.map((value, index) => `${value}:${dlArray[index]}`); console.log('elOriginal:', elArray); console.log('dlOriginal:', dlArray); console.log('dlSorted:', sortedDlArray); console.log(answers.join(' ')); Puede actualizar ${value}:${sortedDlArray[index]} para incluir las clases que necesite allí:
p.ej.
elArray.map( (value, index) => `<div id="wrapper-${index}" class="whatever-class you need here">${value}:${sortedDlArray[index]}</div>` ); elArray = ['k1', 'k2', 'k3', 'k4']; dlArray = ['d4', 'd3', 'd2', 'd1']; answer = ""; console.log("*****************************************"); console.log(elArray) console.log(dlArray) for (let i = 0; i < dlArray.length; i++) { answer += elArray[i]; answer += ':'; answer += dlArray[(dlArray.length - 1) - i]; answer += ' ' } console.log(answer) Hice algunas suposiciones ya que no proporcionó algunos de los detalles de apoyo. Como puede ver aquí, simplemente resté la i actual para obtener el posicionamiento inverso.