1

I have a serious problem understanding the code.

function replace(str, before, after) {

    var rtt = before;
    var rt;

    if(before[0] === before[0].toUpperCase()) {
        var aa = after.split("");
        var u = after[0].toUpperCase();
        aa.splice(0,1,u);
        rt =  aa.join("");    
    }
    else {
       rt = after;
    }

    var r = str.split(" ");  
    var i = r.indexOf(rtt);
    r.splice(i,1, rt);
    var join = r.join(" ");
    return join;
}

replace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

I don't understand why I can't just do r.join(" ") after r.splice(i,1,rt) and then return r? Why I must declare new variable join? otherwise return will not return correct statement. The code is correct though.

3
  • 1
    You could just do r = r.join(" "); instead other than a new variable. But r.join(" ") will return a value and not set it, so that's why. Commented Jul 22, 2015 at 5:19
  • 1
    You can just do r.splice(i,1, rt); return r.join(" ");. Commented Jul 22, 2015 at 5:21
  • I think I know what is a problem. I thought that join method is similar to array mutators, and that it will modify existing array, but it is wrong. Is that right? Commented Jul 22, 2015 at 5:34

2 Answers 2

2

According to MDN,

The splice() method changes the content of an array by removing existing elements and/or adding new elements.

What this method returns ?

An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

As far as this question

Why I must declare new variable join ?

Actually, you don't have to declare a new variable. You can just return this value

return r.join(" ");

function replace(str, before, after) {

    var rtt = before;
    var rt;

    if(before[0] === before[0].toUpperCase()) {
        var aa = after.split("");
        var u = after[0].toUpperCase();
        aa.splice(0,1,u);
        rt =  aa.join("");    
    }
    else {
       rt = after;
    }

    var r = str.split(" ");  
    var i = r.indexOf(rtt);
    r.splice(i,1, rt);
    return r.join(" ");
}

var replaced = replace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

document.write(replaced);

Sign up to request clarification or add additional context in comments.

4 Comments

yes, I know that. But r.splice() modifies r array. after that r should return new modified array r, and it returns it. But after that when I do r.join(" "), it will not return the string, just existing modified array. But it will return a string if I declare a new variable var join = r.join. Why?
Nope, r.splice() would returns a new array, having modified the contents of r. So using r.join then will do that you want. Please check the above snippet, which essentially is your code with altered the return statement as I suggested you do so.
I think I know what is a problem. I thought that join method is similar to array mutators, and that it will modify existing array, but it is wrong. Is that right?
Exaclty, this is the reason :) !
1

Please note that the splice() method changes the original array.

The splice() method adds/removes items to/from an array, and returns the removed item(s).

Now, you can simply continue using the old variable instead of a new var if the array is still required in the due course of the code.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.