0
    var str = "ajdisoiureenvmcnmvm"
    var arr1 = ["e","t","a","i","n","o","h","r","d","q","l","c","u","m","w","f","s","g","y","p","b","v","k","j","x","z"]
    var arr2 = ["m","i","e","n","v","r","d","j","s","o","u","c","b","a","f","g","h","k","l","p","q","t","w","x","z","y"]

What is the most efficient way of replacing the string such that if it contains an "m" replace it with "e", "i" with "t" and so on.

6
  • 1
    "Most efficient" in JavaScript tends to vary wildly according to which engines you use. Just make code that works. If you find a performance problem with it on a specific engine later, worry about it if/when that happens. Commented Jan 11, 2014 at 16:41
  • 1
    Can you change the structure of things? E.g., does it have to be two parallel arrays? Commented Jan 11, 2014 at 16:42
  • well I mean 26 lines of code is definitely not the most efficient way Commented Jan 11, 2014 at 16:42
  • @T.J.Crowder what so you mean Commented Jan 11, 2014 at 16:43
  • 1
    @ user: What makes you say that? Efficient by what standard? Commented Jan 11, 2014 at 16:43

6 Answers 6

1

If you're fine with map:

function encode (string) {
  return string.split("").map(function (letter) {
    return arr1[arr2.indexOf(letter)];
  }).join("");
}
Sign up to request clarification or add additional context in comments.

Comments

0

This should be self explanatory and relatively efficient.

str = str.split("")
for(var i = 0; i < str.length; i++) {
    var c = str[i];
    var j = arr1.indexOf(c);
    str[i] = arr2[j];
}
str = str.join("");

I'd use a mapping for this kind of thing though.

9 Comments

I think it may be this: str[i] = arr2[j];
Indeed, I missed that. This doesn't work. I wonder why @user3184807 said it does.
Strings in JS are immutable so you can't do str[i] = arr2[j];.
using a tmp string and a appending it, will work but it's probably slowish. joining an array is hardly going to be any quicker though
what if indexOf return -1
|
0
var str = "ajdisoiureenvmcnmvm"
var arr1 = ["e","t","a","i","n","o","h","r","d","q","l","c","u","m","w","f","s","g","y","p","b","v","k","j","x","z"]
var arr2 = ["m","i","e","n","v","r","d","j","s","o","u","c","b","a","f","g","h","k","l","p","q","t","w","x","z","y"]

newStr = str.split("");
for (var i=0, len=newStr.length; i<len; i++)
{
    index = arr2.indexOf(newStr[i]);
    if(index>=0)
        newStr[i] = arr1[index];
}
str = newStr.join("");
console.log(str);

Comments

0

Probably a more straight forward approach is to put the original string into an array first so you can manipulate it more easily character by character:

var arr1 = ["e","t","a","i","n","o","h","r","d","q","l","c","u","m","w","f","s","g","y","p","b","v","k","j","x","z"];
var arr2 = ["m","i","e","n","v","r","d","j","s","o","u","c","b","a","f","g","h","k","l","p","q","t","w","x","z","y"];
var str = "ajdisoiureenvmcnmvm";
var data = str.split(""), index;
for (var i = 0; i < arr2.length; i++) {
    index = str.indexOf(arr2[i]);
    if (index >= 0) {
        data[index] = arr1[i];
    }
}
// build the string back again
str = data.join("");

Working demo: http://jsfiddle.net/jfriend00/fAQTe/

1 Comment

you'd be better off doing str.split("")
0

It depends on your definition of "efficient." With that structure, it's quite hard to be efficient. But something like this is functional and short:

var rex = new RegExp("[" + arr1.join("") + "]", "g");
var result = str.replace(rex, function(letter) {
    var index = arr1.indexOf(letter);
    return index === -1 ? letter : arr2[index];
});

Live Example | Live Source

There, we create a regular expression using a character class to match each of the source characters, then do a replacement operation using a function to look up the replacement (if any) from the arrays.

Now, if you can change the structure, there are better ways, like using a map rather than arr1.indexOf. E.g.:

var map = {
    "e": "m",
    "t": "i",
    "a": "e",
    // ...
};

Then (this uses ES5's Object.keys, which can be shimmed if needed):

var rex = new RegExp("[" + Object.keys(map).join("") + "]", "g");
var result = str.replace(rex, function(letter) {
    return map[letter] || letter;
});

Comments

0

I knew you don't ask for the way to do by shell script, but it will give you the feeling how easily it can be done by tr command in this special request.

echo "ajdisoiureenvmcnmvm" |tr "etainohrdqlcumwfsgypbvkjxz" "mienvrdjsoucbafghklpqtwxzy"

exsnhrnbjmmvtacvata

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.