1
I need to compare 2 potentials that are not formatted..
Example:
2,2 = 2²
3,3 = 3³
if (2² > 3³)
the numbers arrive in an array, it comes like this [2,2,3,3], I need to separate them in pairs and I need to turn a power...
1
I need to compare 2 potentials that are not formatted..
Example:
2,2 = 2²
3,3 = 3³
if (2² > 3³)
the numbers arrive in an array, it comes like this [2,2,3,3], I need to separate them in pairs and I need to turn a power...
7
Use Math.pow
to calculate power, and compare results. For example:
var entrada = [2,2,3,3];
// confiando que entrada.length >= 4
if(Math.pow(entrada[0], entrada[1]) > Math.pow(entrada[2], entrada[3])) {
// a primeira dupla é maior
}
Browser other questions tagged javascript
You are not signed in. Login or sign up in order to post.
How do you want to represent them before formatting? Two numbers in an array
[2,2]
, a string"2,2"
, another format?– mgibsonbr
Well, the simplest thing seems to be to calculate them and compare the results:
if ( Math.pow(arr[0], arr[1]) > Math.pow(arr[2], arr[3]) )
. P.S. Could anyone comment on the reason for the votes against?– mgibsonbr
You can use the function
Math.pow(base, expoente);
– ramaral
Our @mgibsonbr, the phrase in my reply was almost identical to your comment.
– bfavaretto
And, if the potentials are too large, which can overflow, Voce can compare the logs.
– Lucas Virgili
Curiosity: If you are interested in the mathematical aspect, How to compare powers: http://math.stackexchange.com/questions/97022/comparing-numbers-in-form-xy
– OnoSendai