Dalesevic
Dalesevic

Reputation: 9

Javascript string.replace not working with my arrays, what am I doing wrong?

So I have three Arrays:

Here's my code, it goes through the function but returns the same as it gets. The goal is for it to replace "W" with "0", "B" with "1" and so on. What am I doing wrong?

function internalArtNrGenerator() {

    var str = new Array("5 1 1 1 0 W 1 1", "5 1 1 1 0 B 1 1", "5 1 1 1 0 K 1 1");
    var replace = new Array("W", "B", "K", "M", "A", "S", "N");
    var by = new Array("0", "1", "2", "3", "4", "5", "6");

    function str_replace(replace, by, str) {
        for (var i = 0; i < replace.length; i++) {
            str = str.replace(new RegExp(replace[i], "g"), by[i]);
        }
        return str;
    }

}

Upvotes: 0

Views: 119

Answers (3)

Jesper
Jesper

Reputation: 7615

Right now you're defining the function str_replace, but you never make use of it. You're saying "if, within internalArtNrGenerator, someone wants to make use of the function str_replace, here's that function", but nothing ever makes use of it.

Reading the code it also looks confused. The idea with a function is that you call it and that you send the data into it by using parameters:

function addTwoNumbers(a, b) { return a + b; } // here, the parameters are a and b
addTwoNumbers(4, 2);

You're using the variable names str, replace and by as if they will automatically map to the parameters with the same name. That's not what happens. Those parameter names are just what the values you send in will be called within that function:

var x = 42; var a = []; var b = "something else";
var result = addTwoNumbers(x, 77); // does not at all touch the variables a and b above
// when addTwoNumbers is being run, a will be 42 (the value of x) and b will be 77
var secondResult = addTwoNumbers(8, 92);
// now when addTwoNumbers is being run, a will be 8 and b will be 92

Upvotes: 1

Abdul Rehman Sayed
Abdul Rehman Sayed

Reputation: 6672

Check this Plunkr : http://plnkr.co/edit/DYNk2kbjJfxGlEDZSnrT?p=preview

You will need to call the function. & since your str is an array & not a string, iterate over it & pass it to the function. (There are better methods for iteration).

function internalArtNrGenerator() {

    var str = new Array("5 1 1 1 0 W 1 1", "5 1 1 1 0 B 1 1", "5 1 1 1 0 K 1 1");
    var replace = new Array("W", "B", "K", "M", "A", "S", "N");
    var by = new Array("0", "1", "2", "3", "4", "5", "6");

    function str_replace(replace, by, str) {
        for (var i = 0; i < replace.length; i++) {
            str = str.replace(new RegExp(replace[i], "g"), by[i]);
        }
        return str;
    }

   for(var x in str)
   {
   str[x] = str_replace(replace,by,str[x]);
   console.log('inside');
   console.log(str[x]);
   }
   return str;
}

var x = internalArtNrGenerator();
console.log('final');
console.log(x);
alert(x);

Upvotes: 0

user3297291
user3297291

Reputation: 23372

You'll have to call the method on a string for it to work. For example, you can map your str array with str_replace to get the results. Note that it's kind of unclear because the str variable name is re-used.

function internalArtNrGenerator() {

    var str = new Array("5 1 1 1 0 W 1 1", "5 1 1 1 0 B 1 1", "5 1 1 1 0 K 1 1");
    var replace = new Array("W", "B", "K", "M", "A", "S", "N");
    var by = new Array("0", "1", "2", "3", "4", "5", "6");

    function str_replace(replace, by, str) {
        for (var i = 0; i < replace.length; i++) {
            str = str.replace(new RegExp(replace[i], "g"), by[i]);
        }
        return str;
    }
  
    // The .bind creates a new method with the first 2 arguments bound,
    // .map passes the last one: the actual string to perform the action on
    // and returns a new array
    return str.map(str_replace.bind(null, replace, by));
}

console.log(internalArtNrGenerator());

Upvotes: 0

Related Questions