Created
August 21, 2018 17:07
-
-
Save davidchoo12/459110374e0bbcf99aa60ad5ae70fb3e to your computer and use it in GitHub Desktop.
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
function shuffle (action, inputString, optionalArgs) { | |
if (action && !parseInt(action)) throw 'invalid action type'; | |
if (action < 1 || action > 4) throw 'action out of range'; | |
if (action == 1) { | |
const end = inputString.slice(-1 * optionalArgs); | |
return end + inputString.slice(0, -1 * optionalArgs); | |
} else if (action == 2) { | |
return inputString.split('').reverse().join(''); | |
} else if (action == 3) { | |
var n = 0; var x = ''; | |
let chars = inputString.split(''); | |
chars.forEach((e) => { | |
const m = inputString.match(new RegExp(e, 'g')).length; | |
if (m > n) { x = e; n = m; } | |
inputString.replace(e, ''); | |
}); | |
return x; | |
} else if (action == 4) { | |
let ordering = optionalArgs.split('').reduce((a, c, i) => {a[c] = i; return a;}, {}); | |
return inputString.split('').sort((a, b) => { | |
return ordering[a] - ordering[b]; | |
}).join(''); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment