Last active
June 5, 2018 00:05
-
-
Save 0D1NTR33/12605e61c77f7a2e0504b65fffe5a70a to your computer and use it in GitHub Desktop.
Duplicate Encoder for string
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
//original | |
function duplicateEncode(word) { | |
let result = ''; | |
let textHash = {}; | |
for (let i = 0; i < word.length; i++) { | |
let key = word.charAt(i).toLowerCase(); | |
switch (textHash[key]) { | |
case true: | |
textHash[key] = false; | |
break; | |
case false: | |
break; | |
default: | |
textHash[key] = true; | |
} | |
} | |
for (i = 0; i < word.length; i++) { | |
key = word.charAt(i).toLowerCase(); | |
if (textHash[key]) { | |
result = result + 'Uni'; | |
} else { | |
result = result + 'Dup'; | |
} | |
} | |
console.log(textHash); | |
return result; | |
} | |
//best | |
function duplicateEncode(word){ | |
return word | |
.toLowerCase() | |
.split('') | |
.map( function (currentValue, index, array) { | |
return array.indexOf(currentValue) == array.lastIndexOf(currentValue) ? 'Uni' : 'Dup' | |
}) | |
.join(''); | |
} | |
//fork | |
function duplicateEncode(word){ | |
return [] | |
.map | |
.call(word.toLowerCase(), function (a, i, w) { | |
return w.indexOf(a) == w.lastIndexOf(a) ? '(' : ')' | |
}) | |
.join(''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment