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
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);