One Day Away explainer


#1

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');
print(message);

Javascript Concepts: Variables, Strings, .replace()

Grasshopper Concepts: print()


#4

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


#5

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');
print(message);

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');
}
print(message);

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;
  }
}
print(message);

That’s a bit messy just to 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”.

–Frankie