0

I'm trying to add a very simple method to the String object:

 String.prototype.double = function double() {
        let newStr = ""
        for (let char of this) {
            console.log(char)
            newStr += char;
            newStr += char;
        }
 }

As you can see, it just "doubles" each character in the string. The problem is, i don't know how to actually replace the original string, with the new one("newStr")

I've tried doing this = newStr, which resulted in an "Invalid left-hand side in assignment" error.

How can i replace the original string, on which the method was called, with the new one?

1
  • 2
    strings are immutable, you need an assignment of the result. Commented Feb 28, 2018 at 19:12

2 Answers 2

4

You need to return the new string.

String.prototype.double = function double() {
  let newStr = ""
  for (let char of this) {
    newStr += char+char;
  }
  return newStr;
}

console.log("string".double());

Other shorter approach could be

String.prototype.double = function double() {
  return this.split("").map(e=>e+e).join("");
}

console.log("string".double());

Update: (regarding OP's comment)

In JavaScript, strings are immutable objects, which means that the characters within them may not be changed and that any operations on strings actually create new strings.

So you wont be able to change the original string as any operation done on a string always returns a new string. So you will need to assign the value back to the original string.

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

1 Comment

Thanks, but i think i wasn't clear enough about my goal: I want the original variable, that holds a string, on which this method is called- to be changed. Example: let's say i have a variable called str, whose value is "abcd". After calling str.souble(), i would liek this variable to hold the value "aabbccdd" . Is it even possible, given what Nina Scholz said, about the immutability of strings?
1

Consider returning new string rather than replacing it. As mentioned, strings are immutable. So your code will be:

String.prototype.double = function double() {
    let newStr = ""
    for (let char of this) {
        console.log(char)
        newStr += char;
        newStr += char;
    }
    return newStr;
}

The additional benefit of doing this is that you can have string methods as "chained". For example:

'ab'.double().toUpperCase(); //-> "AABB"

2 Comments

I see...so it seems, what i'm trying to achieve, is impossible? I have to assign the value to a new variable, when i call the function?
Shortly saying it doesn't make sense. Also, you don't have to assign value to a new variable. The main purpose of any function (in your case double) is to return value or throw exception. Consequently, declaring and using of variable might help you to have a clear code, but it's not mandatory. Hope I've got your question correctly. Hope it helps.

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.