Skip to content

Instantly share code, notes, and snippets.

@0D1NTR33
Last active June 5, 2018 00:05
Show Gist options
  • Save 0D1NTR33/12605e61c77f7a2e0504b65fffe5a70a to your computer and use it in GitHub Desktop.
Save 0D1NTR33/12605e61c77f7a2e0504b65fffe5a70a to your computer and use it in GitHub Desktop.
Duplicate Encoder for string
//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