Min/Max value/item d'un array
Bonjour,
Je cherche a récupérer la valeur maximale et minimale dans un array, ainsi que la position de ces deux derniers,
J'ai écrit ceci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| function max(array, type) {
init = 0;
for (i=0;i<array.length;i++) {
value = parseFloat(array[i]);
if (value > init) {
init = (type == 'indice') ? i : value;
}
}
return init;
};
function min(array, type) {
init = 0;
for (i=0;i<array.length;i++) {
value = parseFloat(array[i]);
if ((value != 0) && (value < init)) {
init = (type == 'indice') ? i : value;
}
}
return init;
}; |
Mais pour l'indice minimum ça ne fonctionne pas :
Code:
1 2 3 4 5 6 7 8 9
| var t = new Array('10','25','-60','-30','55');
min_solde = min(t);//-60
max_solde = max(t);//55
min_solde_indice = min(t, 'indice');//2
max_solde_indice = max(t, 'indice');//4
-60
55
3 //au lieu de 2!
4 |
Je ne comprend pas, que se passe-t-il ?