Created
February 6, 2020 16:19
-
-
Save sajadtorkamani/4eb8a8a9601db0296f258aa8605d4efb to your computer and use it in GitHub Desktop.
Insert 5 digit to obtain maximum value
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
const solution = num => { | |
const isNegativeNum = num < 0; | |
const numStr = Math.abs(num).toString(); | |
const permutations = []; | |
for (let i = 0; i <= numStr.length; i++) { | |
const permutation = Number(numStr.substr(0, i) + '5' + numStr.substr(i)); | |
permutations.push(permutation); | |
} | |
return isNegativeNum | |
? Math.min(...permutations) * -1 | |
: Math.max(...permutations); | |
}; | |
export default solution; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment