One Day Away explainer

Aim of the puzzle: Use the .replace() method to change a string’s value. .replace() takes 2 arguments, 1st is the string you are looking for, 2nd is the string you want instead. If the first string argument is found, that string is removed and replaced with the second string argument.

Walk through of solution: The message variable has the string value ‘Today is my birthday’. The goal here is to change the word ‘Today’ to be ‘Tomorrow’ instead of ‘Tomato’ using the .replace() method.

Tap on ‘mato’ inside the .replace() method. Tap the str button to replace the string and enter the string ‘morrow’. Run the code to see that the message printed is now ‘Tomorrow is my birthday’.

Notice that ‘day’ in ‘birthday’ is not changed. The .replace() method only replaces the first instance of the string it was looking for and stops.

Sample code solution:
(Tap below to reveal)

var message = 'Today is my birthday!';
message = message.replace('day', 'morrow');

Javascript Concepts: Variables, Strings, .replace()

Grasshopper Concepts: print()


There are 2 words having day like Birthday and today , so how we will know that . replace () function will only change the Today word.


The .replace() method will always find the 1st match in the string. The last sentence in the description mentions this, but it’s easy to overlook. Another way to find this out is to simply play around with the function.

Below, I explain how you could do a “replace all”.

The code below uses .replace() twice which means both of the 'day's will turn into 'morrow's

var message = 'Today is my birthday!';
message = message.replace('day', 'morrow');
message = message.replace('day', 'morrow');

This will print out Tomorrow is my birthmorrow.

Normally, you wouldn’t want to write out a new line of code for replacing the same word over and over. You could use a loop that runs as long as the string includes 'day':

var message = 'Today is my birthday!';
while (message.includes('day')) {
  message = message.replace('day', 'morrow');

If you don’t know what a While Loop is, you can do the same thing with a For Loop:

var message = 'Today is my birthday!';
for (let i = 0; i < 1; i) {
  if (message.includes('day')) {
    message = message.replace('day', 'morrow');
  } else {
    i = 1;

That’s a bit messy just to do a “replace all” on a string. A better way to do this is to use a Regular Expression rather than a string. Regular Expressions are used to find a pattern in a string. For example, you can find all the numbers in a string, or all the capital letters, or all the letter 'b’s, or every word longer than 4 letters. Instead of searching the message for the string day, you can search for all the matches of ‘day’ using a regular expression that looks for all of the 'day' patterns in the string:

message = message.replace(/day/g, 'morrow');

A regular expression is surrounded by Forward Slashes instead of quotes. So, /day/ is a regular expression that searches for the string 'day'. If you want to find all the matches for 'day', you add a g at the end which stands for Global. That means, “search my whole string for all the matches”.



why dont it work when we replace ‘today’ with ‘tomorrow’

This puzzle will actually complete if you any of these replacements:

  • 'day' --> 'morrow'
  • 'oday' --> 'omorrow'
  • 'Today' --> 'Tomorrow'

But it won’t work with 'today' --> 'tomorrow', because the strings are case-sensitive. There is no 'today' in the message, only 'Today' with a capital 'T'.



Correct me if I am wrong … My doubt is even the word birthday has day in it … Why that is not getting replaced ???


1 Like

Hey there, I moved your post into this thread.

The .replace() method only replaces the very first instance it finds. For an explanation of how to replace all instances in a string, take a look at the post by @Grasshopper_Frankie earlier in this thread.

Hope this helps!

1 Like

Interesting. So does /day/ without the g do the same as 'day', and just replace the first instance?

Correct! RegEx gives you the option of making multiple matches, while a string limits you to one match only.

Looking at the MDN page for .replace(), it says:

“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.”

1 Like

Hi, l am a Teeny-Tiny-Bitty-Bit stuck. This is what I have added----

Var message =

‘Today is my birthday!’;
message =message. replace(
‘mato’, 'morrow ';

          I guess that's right, because I followed the instructions of those little blue notices. But it's giving--

“The starter code has been edited, use the reset button to reset the puzzle.”
So have I really edited the puzzle?

Hey, are you a kid?!
I am too! :purple_heart:

Hey there, the code you typed looks pretty good to me! Try posting a screenshot, that might show us what the problem is.

Also, check that message is spelled correctly and is all lowercase. If it’s been renamed or is capitalized, that might cause the “reset needed” error message to appear.

Hope this helps!

Oh! I got it this way-

var message =
‘Today is my birthday!’ ;
message =message.replace (‘day’, ‘morrow’);
print (message);

It was quite easy! :yum:

1 Like

What is the difference between an argument and a string?

– V

I cant move on to the next lesson it keeps saying The starter code has been edited or removed. Use the “reset” button to reset the puzzle.

Uploading: 2020-12-10 (5).png…

my app is brocken

Can someone help ? I’m not sure why it’s saying it is incorrect.