0

I need to find which variable has the largest value.

Normally I would do something like:

var Dimensions = [Height, Length, Depth];

var biggestSide = Math.max.apply(Math, Dimensions);

But I need to do something with the variable after this.

Is there anyway to identify the var with the largest value without using arrays or a series of if statements?

4
  • need to do something with the variable after this - Why would that stop you from using the Math.max route? Commented Aug 27, 2015 at 8:02
  • what do you want to do? what is the meaning of "identify"? can you expand your question? Commented Aug 27, 2015 at 8:04
  • @mplungjan - in brief, the vars Height, Length and Width and the max values from arrays themselves. I need to identify the array that has the largest value so I can do something with the vars that are pushed to that array. Commented Aug 27, 2015 at 8:12
  • 1
    so you have multiple arrays and you want to find the array containing the largest single element? if yes, you better edit your question Commented Aug 27, 2015 at 8:14

2 Answers 2

1

You could use a switch statement

var biggestSide = Math.max(Height, Length, Depth);

switch (biggestSide) {
     case Height:
         ...
         break;
     case Length:
         ...
         break;
     case Depth:
         ...
         break;
}

After reading your comment:

@mplungjan - in brief, the vars Height, Length and Width and the max values from arrays themselves. I need to identify the array that has the largest value so I can do something with the vars that are pushed to that array. – MeltingDog

I think something like this is what you are after

var maxHeight = Math.max.apply(Math, Height);
var maxLength = Math.max.apply(Math, Length);
var maxDepth = Math.max.apply(Math, Depth);

var biggestSide = Math.max(maxHeight, maxLength, maxDepth);

switch (biggestSide) {
     case maxHeight:
         // Do something with Height
         break;
     case maxLength:
         // Do something with Length
         break;
     case maxDepth:
         // Do something with Depth
         break;
}

Note with ES6 you can use the spread operator instead of the apply function:

var maxHeight = Math.max(...Height);
Sign up to request clarification or add additional context in comments.

3 Comments

var biggest = (function(){var maxValueInArray = x.sort().reverse()[0]; return max;})()
Disadvange of my method, it is not clear on fisrt look to guess goal. Better to add comment what actually doing this function;
@simar - Another disadvantage, it modifies the array, so if the array is later required it's no longer at the same state it was. Another one - it's significantly slower & cumbersome.
0

Math.max() accepts as many values as you want. You don't need an array for this:

var biggestSide = Math.max(Height, Length, Depth);

2 Comments

I think he's looking to identify the specific variable thats largest, not the value of the largest.
@JamesA - I guess you're right, I also noticed that after answering, but then the question is just confusing and needs to be expanded to further explain what is expected to be done. In any case, for the explicitly asked question of "... without using arrays ...", I think this is an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.