I have thirty variables which are all numbers:
h0 = 23
h1 = 27
h2 = 90
...
How do I find the largest variable?
The output here should be h2.
I have thirty variables which are all numbers:
h0 = 23
h1 = 27
h2 = 90
...
How do I find the largest variable?
The output here should be h2.
Assuming the variables are all named in consecutive order, you can add them to an array:
var values = [h0, h1, ....h29];
Then iterate over the array, compare the values and keep track of the index of the max value:
var maxIndex = 0;
for (var i = 1; i < values.length; i++) {
if (values[maxIndex] < values[i]) {
maxIndex = i;
}
}
maxIndex
will now contain the index of the maximum value, and you could concat it with 'h'
, e.g.
console.log('h' + maxIndex);
Of course this approach makes a lot of assumptions and is fragile, but that's what you get when doing this kind of "meta-programming" (the variable name really should not be of any concern to anybody using the app).
Using an object would make it a bit better, at least concerning the variable name:
var values = {
h0: h0,
h1: h1,
...
};
var maxProp = '';
for (var prop in values) {
if (values[maxProp] < values[prop]) {
maxProp = prop;
}
}
Put them in an array instead of individual variables, then loop over the array.
To brute-force it, compare adjacent pairs of variables, then pairs of the results, until only one is left.
If you want the answer to be 'h2' instead of '90' (per comment), try using an Object instead of separate variables (or an array, as mentioned in other answers).
var find_max = {
'h0': 23,
'h1': 27,
'h2': 90,
// and so on
};
Then loop over the properties of the object to find the maximum.
var max;
for (var h in find_max) {
if (!max || (find_max[h] > find_max[max])) {
max = h;
}
}
You can probably improve this loop - try using hasOwnProperty
to make sure that the only items in the loop are the ones you want.
You can get it easily by checking the window object using the bracket notation this way:
h0 = 11;
h1 = 24;
h2 = 28;
h3 = 345;
h4 = 1;
var i = 0;
var max = {
name: 'none',
val: 0
};
while (nextItem = window['h'+i]) {
if (nextItem > max.val) {
max = {
name: 'h'+i,
val: nextItem
};
};
i++;
};
document.getElementById('max').innerHTML = max.name + ' = ' + max.val;
<p>Maximum is: <span id="max"></span></p>
EDIT: I was too hasty in posting the below as it didn't take into account the real question. Since then there have been good answers presented so I'll just show a way that you can sort the variables in descending order. One way would be to place the numbers in an array of objects such that:
numArray = [{name:h0, value:23}, {name:h1, value:27}, {name:h2, value:90}];
And do:
numArray.sort(function(a, b){
return b.value - a.value;
});
According to this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
It looks pretty easy once you have the numbers in an array. You could call
Math.max.apply(null, numArray)
on the array, or, using ES6, you could say Math.max(...numArray) (... is the "spread" operator).
You could also do a sort:
numArray.sort(function(a, b){
return b - a;
});
Which would sort your array in descending order with the maximum number now at numArray[0].