Message Mixer

Hi guys !!! If you have already made the project about ‘Message Mixer’ I have a question.

After reading the code below:

I don’t understand the utility of the ‘replaceAllOccurrences’ function (line 41) because I made the following changes and It worked the same way:


I deleted the function ‘replaceAllOccurrences’ and at line 46 (that was line 48 on the first picture) I replaced the function replaceAllOccurrences by the function ‘replaceFirstOccurence’ to reuse It and It worked properly. The message has been encoded the same way.

Can you explain to me why the 'replaceAllOccurrences function was written then ?

Thank you ! :smiley: :raised_hands:

This worked here, but you also only had one ‘I’ and one ‘A’ that needed to be replaced. What would happen if you had multiple ones?

1 Like

Yes ! You are totally right It didn’t work with multiple ‘I’ and ‘A’. But I don’t understand why. Does the ‘replace’ function only replace one element ?

There may be a special way to use it, but from experimenting I have found the .replace() function will only work once and then the line of code is fulfilled.

In order to use it multiple times, you have to contain it in a loop.

1 Like

Yes It’s exact, I made some tests and It just worked once.

Thank you @8-bitgaming !!! :raised_hands:

1 Like

Your welcome!! Happy coding :grinning:

1 Like

You could read documentation for the function you’re using. You probably shouldn’t expect to be able to use something correctly if you haven’t first read what it promises to do.

(also, there are several differences in your two outputs in your screenshots - in the future you’ll have to compare things with some exact method)

What is the point of this message ? I already got my solution. Thank you.

The advice of “you should refer to the documentation” is widely applicable; the next time you run into a problem using a built-in object method, the ways you could fix it include:

  • come here and ask us,
  • read the docs to see how the method works, and whether that aligns with what you want to use it for.

ionatan is simply providing advice, where @8-bitgaming has provided you with a specific solution to this particular problem. :slight_smile:


@ionatan is offering very good advice. It is okay to ask for help, but learning to look up the information yourself certainly helps you get better at coding.

However in this particular case, using MDN, I could not find the documentation saying that this function would only work once. I learned this from experimenting. Is there a better place I could look @thepitycoder?

1 Like

It’s easy to overlook, but it’s in the docs on MDN.


The replace() method returns a new string with some or all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function to be called for each match. If pattern is a string, only the first occurrence will be replaced.

Emphasis is mine.

If you’re handing .replace() a string for the pattern it’s meant to replace, it only replaces the first one. If you want to do several replacements in one pass, you need a regex (or a loop - which may be easier for novices).

**Edit: ** Taking a closer look at the OP’s post, though, their assertion that their reworked code is equivalent is also false.

In their first screenshot, the final line of output on the console reads:
Wh@t !$ the c0l0r 0f the $ky?.

In the second screenshot, where they’ve done away with the replaceAllOccurrences() function in favour of looping over the replacements and only changing the first one, the final line of output reads:
Wh@t !$ the c0lor of the sky?.

These are, quite plainly, not the same…

1 Like

hi, i was running into an issue and found the solution, but I don’t know why/the reason behind this line of code. can someone help explain the difference between

export default MessageMixer;


module.exports = MessageMixer;

this was the one line of code that was giving me an error when I tried to displayMessage() in message.js. I tried looking up the reason why, but people online say that module.exports is actually the correct export format when i am importing like:
import {} from fileName`;

any ideas/advice?

Hi there.

exports default MessageMixer is the ES6 way of working with modules. module.exports = MessageMixer is the older (original) way that modules were introduced.

Which one you use would inform the corresponding import.

Since the import { named, imports, here } from './sourceFile.js format is the ES6 variant, I don’t think you would use Node’s module.exports in the module source; you’d use export.