multidimensional array - Javascript - Compare table content -
i'm trying make video game table in javascript compare statistics between weapons , i'm noob in development !
for moment results of code :
and want got " + " when number higher 0 :
my code don't know how compare weapon 1 weapon 2 , fill last columns :( , there can me ?
<!doctype html> <html> <head> <meta charset="utf-8"> <title>construction dynamique</title> <style> table, tr, th, td { border: 1px solid black; border-collapse: collapse; text-align:center; vertical-align:middle; width:400px; } </style> </head> <body> <select id="weapon_1" onchange="showdetail(1, this.value);"> <option>choice</option> </select> <select id="weapon_2" onchange="showdetail(2, this.value);"> <option>choice</option> </select> <br><br> <script> multiarray = []; multiarray.push( ["arx160", 89, 27, 61, 81, 92, "arx160.png", 83, 35]); multiarray.push( ["tar21", 89, 27, 61, 81, 92, "tar21.png", 83, 35]); multiarray.push( ["g36gl", 89, 27, 61, 81, 92, "g36gl.png", 83, 35]); multiarray.push( ["cz 805 a2", 80, 33, 61, 71, 84, "cz805a2.png", 83, 35]); function fctsort(a, b) { return (a[0].touppercase() > b[0].touppercase()) ? 1 : - 1; } /** * fonction de remplissage d'une colonne du tableau * @param {number} col - colonne concernée par le remplissage * @param {number} ind - index dans le tableau de données */ function showdetail(col, ind) { var odest = document.queryselector('#compare'); // récup. de la table ayant pour id=compare var ordre = [0,6,1,2,3,4,5,7,8]; // ordre d'affichage des données dans le tableau var orows = odest.rows; // collection des lignes var indweapons = ind > 0 ? ind - 1 : null; // récup. index des données à traiter var weapon = multiarray[indweapons]; // pointe les données à afficher var i; // variable de boucle var nb = ordre.length; (i = 0; < nb; += 1) { if( ordre[i] !== 6){ orows[i].cells[col].textcontent = weapon ? weapon[ordre[i]] : ''; } else { orows[i].cells[col].innerhtml = weapon ? '<img src="' +weapon[ordre[i]] +'">' : ''; } } // rempli la dernière colonne calculecart(); } // fonction addoption function addoption(id_dest, data) { var odest = document.queryselector('#' + id_dest); var option; var i; var nb = data.length; console.log(id_dest, data); (i = 0; < nb; += 1) { option = new option(data[i][0], + 1); // on change -> data[i][0] en + 1 odest.add(option); } } // tri des données multiarray.sort(fctsort); // remplissage <select> addoption('weapon_1', multiarray); addoption('weapon_2', multiarray); //--> </script> <!--début création tableau--> <table id='compare'> <tr> <th>weapons</th> <td> </td> <td> </td> <td> points</td> </tr> <tr> <th>image</th> <td> </td> <td> </td> <td> </td> </tr> <tr> <th>accuracy</th> <td> </td> <td> </td> <td> </td> </tr> <tr> <th>power</th> <td> </td> <td> </td> <td> </td> </tr> <tr> <th>range</th> <td> </td> <td> </td> <td> </td> </tr> <tr> <th>rate of fire</th> <td> </td> <td> </td> <td> </td> </tr> <tr> <th>stability</th> <td> </td> <td> </td> <td> </td> </tr> <tr> <th>mobility</th> <td> </td> <td> </td> <td> </td> </tr> <tr> <th>clip size</th> <td> </td> <td> </td> <td> </td> </tr> </table> <!--fin du tableau--> </body> </html>
best regards , , thank !
simply iterate on 2 elements @ same time , store difference:
function compare(a,b){ var result = [""]; for(var = 1; < math.min( a.length, b.length ); i++){ var tmp = a[i] - b[i]; result[i] = (tmp>0?"+":"")+tmp; } return result; }
so 1 can do:
compare(multiarray[0],multiarray[1])
and result comparison row.
if es6:
const compare = (a,b) => a.map((el,i)=>i?el-b[i]:"") .map(n => ((n && n>0)?"+":"")+n);
Comments
Post a Comment