Last active
May 6, 2016 19:42
-
-
Save rootasjey/760ce600fe5c5c0fae5a670dc5121b62 to your computer and use it in GitHub Desktop.
A function which takes two numbers and tells which number is bigger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A function which takes two arrays in argument and tells which number is bigger | |
* The first array is composed of the number and its power as the second array | |
* @param {array} n1 - an array of two string | |
* @param {array} n2 - an array of two string | |
* @return {number} - -1 if n1 > n2, 1 if n1 < n2, 0 if n1 = n2 | |
* A sample input could be : n1 = ['221', '134'], n2 = ['9', '300'] | |
* To understand the algorithm used: | |
* see http://villemin.gerard.free.fr/Wwwgvmm/Analyse/PuissCom.htm | |
*/ | |
function ComparePowers(n1, n2) { | |
// Convert string to number | |
var number1 = parseInt(n1[0]); | |
var power1 = parseInt(n1[1]); | |
var number2 = parseInt(n2[0]); | |
var power2 = parseInt(n2[1]); | |
// Calculate the power divided | |
var powerDivided = power1/power2; | |
var number1Powered = Math.pow(number1, powerDivided); | |
// Compare | |
if (number1Powered > number2) return -1; | |
else if (number1Powered < number2) return 1; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment