Last active
February 24, 2016 09:27
-
-
Save kahwee/4de6cd61b375a343b69b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function reverse (string) { | |
return string.split('').reverse().join('') | |
} | |
function largerHexxeh (kAsString) { | |
var len = kAsString.length | |
var k10 = parseInt(kAsString, 16) | |
var firstHalf = kAsString.slice(0, Math.ceil(len / 2)) | |
var firstHalfInt = parseInt(firstHalf, 16) | |
var firstHalfA = firstHalfInt.toString(16) | |
var firstHalfB = (firstHalfInt + 1).toString(16) | |
var candidates | |
// we need to determine if this is input is larger than k | |
// we are going to cheat here | |
// | |
if (len % 2 === 0) { | |
candidates = [ | |
firstHalfA + reverse(firstHalfA), | |
firstHalfB + reverse(firstHalfB) | |
] | |
} else { | |
candidates = [ | |
firstHalfA.slice(0, -1) + reverse(firstHalfA), | |
firstHalfB.slice(0, -1) + reverse(firstHalfB) | |
] | |
} | |
if (parseInt(candidates[0], 16) > k10) { | |
return candidates[0] | |
} else { | |
return candidates[1] | |
} | |
} | |
console.log(largerHexxeh('1c3')) | |
console.log(largerHexxeh('1331')) | |
console.log(largerHexxeh('d25a')) | |
console.log(largerHexxeh('ffff')) //this is edge case... need to handle this too | |
// output | |
// ~ ❯ node hello.js | |
// 1d1 | |
// 1441 | |
// d33d | |
// 100001 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment