Skip to content

Instantly share code, notes, and snippets.

@sajadtorkamani
Created February 6, 2020 16:19
Show Gist options
  • Save sajadtorkamani/4eb8a8a9601db0296f258aa8605d4efb to your computer and use it in GitHub Desktop.
Save sajadtorkamani/4eb8a8a9601db0296f258aa8605d4efb to your computer and use it in GitHub Desktop.
Insert 5 digit to obtain maximum value
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