0

I have this code which im echoing data from jquery success .

   for(i = 0;i < dat.id.length; i++){

           // im echoing data here 
    }

and i want check for the highest value in dat.voteup

like that (It works)

    var maxvoteup = 0; 
    if (dat.voteup[i] - dat.votedown[i] ==maxvoteup) {
                    console.log(maxvoteup);
        //i want fetch maxvoteup here

     }

But i want make just one For loop not two . How can i do that pls?

EDIT:

  var maxvoteup = 0; 
    for (i = 0;i < (dat.voteup-dat.votedown).length ;i++) {
         if (dat.voteup[i] - dat.votedown[i]>maxvoteup) {
            maxvoteup = dat.voteup[i] - dat.votedown[i];
                    console.log(maxvoteup);
        //i want fetch maxvoteup here
      }
     }

EDIT2: this is what im getting in my jquery response , But actually my max vote is 10 not 8 . 8 is just voted up to 8 , but there is other votes which is 10.

   {"status":"success","message":[],"date_edited":[2016-04-21 21:31:25],"voteup":[],"votedown":[8],"id":[]}
Scooter Daraf
  • 525
  • 7
  • 23
  • you just use both condition inside single for loop .store both result in different variable or array. – JYoThI Apr 21 '16 at 05:49
  • @Tushar sorry for my late answer, maybe i was not clear, i edited my question. i mean i have for loop also to look for max value. how do i combine that with the first for loop – Scooter Daraf Apr 21 '16 at 19:01

1 Answers1

1

Your code is a little hard to read, but if I understand correctly I believe all you need to do is combine both into a single loop with maxvoteup defined outside.

var maxvoteup = 0;
for(i = 0;i < dat.id.length; i++){
    // echo data...

    // max upvote
    if (dat.voteup[i] - dat.votedown[i] == maxvoteup) {
      console.log(maxvoteup);
    }
}

EDIT:

Unfortunately getting the maximum value from a list requires you to iterate through the list, i.e., a maximum value cannot be found in constant time. I suggest you first find the maximum, and then proceed with your for loop.

Also, if you know your maximum is a list of numbers, you can actually use Javascript's apply to make the code a little cleaner:

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

See here: How does the Math.max.apply() work?

EDIT2:

If you want to continuously keep track of the maximum, then all you need to do is move the maxvoteup variable to outside of your response handlers so you can always keep track.

// global scope...
var maxvoteup = 0;

// your jquery response handler
var onSuccessData = function(data) {
  // get the highest possible max from the `voteup` and `votedown` lists
  var responseMax = Math.max.apply(Math, data.voteup.concat(data.votedown));

  // check if it's greater than our global maxvoteup
  if (responseMax > maxvoteup) {
    // if it is, then update our global `maxvoteup` to the new value
    maxvoteup = responseMax;
  }

  // continue with your other processing...
};
Community
  • 1
  • 1
smaili
  • 1,245
  • 9
  • 18
  • Thanks for the explanation. I've updated my answer. – smaili Apr 21 '16 at 19:20
  • Can you edit your question and give an example of what your jquery success data looks like? That will help a lot. – smaili Apr 21 '16 at 19:34
  • i edited my question dear smaili . im getting just the edited values not all values – Scooter Daraf Apr 21 '16 at 19:38
  • I don't understand...how can your maxvote be 10 if the response has 8? – smaili Apr 21 '16 at 19:39
  • i want to highlight the message having maxvote . some messsage is beeing voted to 8 , but actually there is other message which is voted to 10 . so i want to highlight that message not the one with 8. the one with 8 is just geting voted up but its not max . – Scooter Daraf Apr 21 '16 at 19:41
  • I've added a second edit. If that answers your question, then please mark my answer. Otherwise, you probably need additional help that is outside the scope of your question. – smaili Apr 21 '16 at 19:49
  • I think i was wrong with my idea , because im not getting back the maxvalue to compare . i think i have to edit my jquery response and add max value to bring it back from database and then i can compare if its max or not. . – Scooter Daraf Apr 21 '16 at 19:51
  • But anyway +1 for the math max i will use it when i get the maxvalue from database , thanks – Scooter Daraf Apr 21 '16 at 20:02