Scooter Daraf Scooter Daraf - 7 months ago 9
Javascript Question

each function to get the max of array of differences

I have this sample example here .

<div class="subchatarea2">
<div class='message_holder user45 chatid28'>
<div class='likescore'>5</div><div class='unlikescore'>0</div>
</div>

<div class='message_holder user46 chatid29'>
<div class='likescore'>6</div><div class='unlikescore'>1</div>
</div>
<div class='message_holder user47 chatid30'>
<div class='likescore'>4</div><div class='unlikescore'>0</div>
</div>
<div class='message_holder user48 chatid31'>
<div class='likescore'>6</div><div class='unlikescore'>2</div>
</div>
</div>


and my js is

var his = $('.subchatarea2 .message_holder');

var maxvoteups = [];
var diff = his.children('.likescore').text()-his.children('.unlikescore').text();

his.each(function() {

maxvoteups.push(diff);
});
var maxvoteup = Math.max.apply(Math, maxvoteups);
alert(maxvoteup);


My wish is to get this result (array of differenses between upvote and downvote) like that :

[5,5,4,4]


But in my page im getting

[5544,5544,5544,5544]


and when i created this fiddle it gaves the result you are seen.

How can i get my wish result
[5,5,4,4]
because in further i wanna select the max upvoted with min downvoted

var maxvoteup = Math.max.apply(Math, maxvoteups);


and then i wanna get the first
5
because the second 5 has downvote 1 and the first one has downvote 0 .

How can i accomplish this thanks ?

Answer
 var his = $('.subchatarea2 .message_holder'); 

 var maxvoteups = []; 

 his.each(function() {
  var diff = $(this).children('.likescore').text() - $(this).children('.unlikescore').text();
  var downvotes = $(this).children('.unlikescore').text();
  maxvoteups.push([diff, downvotes]);
});

Now you have a list like this :

[[5,0], [6,1], [4,0],[6,2]]

wich is [[totalvotes(total_up - total_down), total_downvotes]]

EDIT : Better than this is make a list of objects :

 var his = $('.subchatarea2 .message_holder'); 

 var maxvoteups = []; 

 his.each(function() {
  var diff = $(this).children('.likescore').text() - $(this).children('.unlikescore').text();
  var downvotes = $(this).children('.unlikescore').text();
  maxvoteups.push({total : diff, down : downvotes});
});

Then a function to compare objects :

function compare(a,b) {
  if (a.total > b.total)
    return -1;
  else if (a.total < b.total)
    return 1;
  else if (a.down > b.down) // If a has more downvotes the a is under b.
    return -1;
  else if (a.down < b.down)
    return 1;
}

Now order the list like this :

maxvoteups.sort(compare);

EDIT

Final code :

var his = $('.subchatarea2 .message_holder');

var maxvoteups = [];

his.each(function() {
  var diff = $(this).children('.likescore').text() - $(this).children('.unlikescore').text();
  var downvotes = $(this).children('.unlikescore').text();
  var upvotes = $(this).children('.likescore').text();
  var node = $(this);
  maxvoteups.push({total : diff, downvotes : downvotes, upvotes : upvotes, node : node});

});
//var maxvoteup = Math.max.apply(Math, maxvoteups);
function compare(a,b) {
  if (a.total > b.total)
    return -1;
  else if (a.total < b.total)
    return 1;
  else if (a.down < b.down)
    return -1;
  else if (a.down > b.down)
    return 1;
}
maxvoteups.sort(compare);
alert('The max value is : ' + maxvoteups[0].total + ' with ' +  maxvoteups[0].upvotes + ' upvotes and ' +  maxvoteups[0].downvotes + ' downvotes and is the jquery element ' +  maxvoteups[0].node );

maxvoteups.forEach(function(post, index){
    alert('The' + (index + 1) + ' value of the sorted list is : ' + post.total + ' with ' +            post.upvotes + ' upvotes and ' +  post.downvotes + ' downvotes and is the jquery element ' +  post.node );
});
Comments