Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

can someone explain me the replace() function please?

I tried to read about the replace function on js in the MDN, but I still don't know how to use it, is it possible to use replace() with objects literal? I mean, if I will take an alphabet letter that is equal to a key in my object literal, is it possible to "replace()" that letter into the value of the property that is equal to the alphabet letter?

also, does replace() only works with Regular expressions? I am not sure that I can compare something to a key at all after all a key is like a variable...

have a good day ^-^

Idan.

2 Answers

Steven Parker
Steven Parker
243,253 Points

The "replace" method works on strings, not objects. But it could be used on an object property if that property was a string. And be aware that it does not change the string it is applied to, but returns a new string containing the changes.

The "match" argument (first argument) can be either a regular expression or a plain string.

thanks a lot, another question, if I want to take a string and change it, let's say the a=b b=c c=d and so on... can I do that without the replace(), can I use the object literal for that?

^-^ thanks again.

Steven Parker
Steven Parker
243,253 Points

You certainly don't need "replace" if you're reassigning the entire string.

Can you show me an example of what you're intending to do using an object literal? I'm not sure I understand that part of the question.

I want to do something like that:

I will pass in the string: "my name" and it will show me "nz obnf" n is coming after m in the alphabet z is coming after y b is after a and f is after e, so I am asking if I can put all of the alphabets in an object literal like that:

function replaceHim(str) {
    let alphabetArray = {
        a: 'b',
        b: 'c',
        c: 'd',
        d: 'e',
        e: 'f',
        f: 'g',
        g: 'h',
        h: 'i',
        i: 'j',
        k: 'l',
        l: 'm',
        m: 'n',
        n: 'o',
        o: 'p',
        p: 'q',
        q: 'r',
        r: 's',
        s: 't',
        t: 'u',
        u: 'v',
        v: 'w',
        w: 'x',
        x: 'y',
        y: 'z',
        z: 'a'
    };

and when you will pass in the string "my name"(in the function) it will look for the letters value and output it like that "nz obnf", sorry, I am not the best at explaining things... I hope its clear this time...

Steven Parker
Steven Parker
243,253 Points

Sure, just a little more and you'll have a working function:

// ...code the same to here...
        j: 'k',  // this was missing
// ...
        z: 'a',
      ' ': ' '   // don't forget to map space to itself!
    };
    return str.split('').reduce((s,a) => s + alphabetArray[a], '');  // return the converted input
}

Just remember that "alphabetArray" might look like an array being indexed, but it's actually a literal object accessed with bracket membership notation.