Get the highest variable number

I'm trying to get the highest variables out of these in a condition, how would l do that?


$gameParty.members()[0]._zxc = Math.randomInt(100 * $gameParty.members()[1].tgr)


$gameParty.members()[1]._zxc = Math.randomInt(100 * $gameParty.members()[1].tgr)


$gameParty.members()[2]._zxc = Math.randomInt(100 * $gameParty.members()[1].tgr)


if ("highest variable number === $gameParty.members()[0]._zxc) {


do this }


if ("highest variable number === $gameParty.members()[1]._zxc) {


do this }


if ("highest variable number === $gameParty.members()[2]._zxc) {


do this }
Hi!


Most of the time code is just about putting your problem into words:


You want to know what the maximum value of a suite of numbers is: how would you do it yourself on paper?


Probably you'd start by looking at the first value and remembering it. Then you'd check the second, and if it is higher remember that one instead and that it's the second.


Then you'd repeat the process with as many values you have, progressing one by one.


In code you'd do the same thing.


I suggest trying to do it yourself following the steps I described above, you'll learn more this way.


In case you are stumped: below is a way to do it, generalized for any number of values.

Spoiler
var variable //define your array
var index = 0; //start: check index 0
var currentMax = variable[0]; //set maximum to the value at index 0

for (var i = 0; i <variable.length; i++) { //for the entire array
if (variable > currentMax) { //if the value of the checked index is > to the current maximum value
index = i; //new index
currentMax = variable; //new currentMaximum
}
}

// index will be the index of the max Value
// currentMax will be the actual value;
// Then you coud do a condition about the index:
if (index === 0) {do something} // if maximum value is inside variable[0] do thing




Hope I helped, and good luck with your coding!
you can also compare variables to each other.


A possible condition would be: Var A > Var B && Var A > Var C


Hope I could help 🙂
Yeah, I'm having a very hard time with array even after years of using rpg maker. Probably because l have no idea what i++ mean or how to write it.


For example, someone write this to me to determine which party member had the highest TP and I still have no idea how it even works


if ($gameParty.members().sort(function(b1,b2){


return (b1.tp > b2.tp) ? 1 : ( (b1.tp < b2.tp) ? -1 : 0 );


}).pop() === user) {


condition = true;


}


@silverskin


yeah, already did that. I still need arrays though when i'm going to use a lot more variables.
i++ means increase the value of i by 1;


It's a short way of writing i += 1;


You don't need to know every single thing in javascript to be able to do what you want. You can write a for loop without knowing what i++ means.


Below is an explanation, using my limited understanding and very little experience with sort, of how the function you pasted works:


$gameParty.members().sort(function) //this will sort the array $gameParty.members() in increasing order using the function provided.
//The function provided is:
function(b1,b2){
return (b1.tp > b2.tp) ? 1 : ( (b1.tp < b2.tp) ? -1 : 0 );
}
//a ? b : c is short for if(a) {b} else {c} So it could also be written
if (b1.tp > b2.tp) { //if member 1 tp > member 2 tp
1
} else {
if (b1.tp < b2.tp) { //if member 1 tp < member 2 tp
-1
} else { //if member 1 tp = member 2 tp
0}
}
//Explaining how sort works exactly is a little complicated, you can take a look at this page: it explains it well
// http://www.w3schools.com/js/js_array_sort.asp

//So now we have sorted your array in increasing order
array.pop() //removes the last thing in an array and returns it.
//So our expression
$gameParty.members().sort(function(b1,b2){return (b1.tp > b2.tp) ? 1 : ( (b1.tp < b2.tp) ? -1 : 0 );}).pop()
//will return the party member with the maximum tp.

//The next line is just a basic conditional to check if the expression returns the user, and if it does, make a condition true.
if (expression === user) {condition = true;}


I hope this clarified things for you.


You could have written this using a loop just as I did above, or in many other ways probably.


Edit: here is a solution using sort for your problem, untested but it should work:


$gameParty.members().sort(function(a, b){return a._zxc - b._zxc});


This will sort the party members by the value of their "_zxc" property.
You'll then be able to use the array.pop() or array.shift() function to return the first or the last element.

Original thread: https://forums.rpgmakerweb.com/threads/get-the-highest-variable-number.70738/