From 3b0c6c517269ea96e76c7a699b9fbf1a25e95422 Mon Sep 17 00:00:00 2001 From: Clarence Bakosi Date: Wed, 29 Oct 2025 19:14:34 +0100 Subject: [PATCH] feat(curriculum): Add interactive examples to How Can You Replace Parts of a String with Another (#63229) --- .../67326c29dcd98fc5ecc49779.md | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/curriculum/challenges/english/blocks/lecture-working-with-string-modification-methods/67326c29dcd98fc5ecc49779.md b/curriculum/challenges/english/blocks/lecture-working-with-string-modification-methods/67326c29dcd98fc5ecc49779.md index 53923b01654..3ed8207c425 100644 --- a/curriculum/challenges/english/blocks/lecture-working-with-string-modification-methods/67326c29dcd98fc5ecc49779.md +++ b/curriculum/challenges/english/blocks/lecture-working-with-string-modification-methods/67326c29dcd98fc5ecc49779.md @@ -5,7 +5,7 @@ challengeType: 19 dashedName: how-can-you-replace-parts-of-a-string-with-another --- -# --description-- +# --interactive-- In JavaScript, there are many scenarios where you may need to replace a portion of a string with another string. @@ -23,35 +23,47 @@ string.replace(searchValue, newValue); The `newValue` is the value that will replace the `searchValue`. Here's a simple example: +:::interactive_editor + ```js let text = "I love JavaScript!"; +console.log(text); // "I love JavaScript!" let newText = text.replace("JavaScript", "coding"); - -console.log(newText); // Output: "I love coding!" +console.log(newText); // "I love coding!" ``` +::: + In this example, the word `JavaScript` is found within the string and is replaced with `coding`. The `replace()` method is case-sensitive, meaning that it will only find exact matches of the `searchValue`. For example: +:::interactive_editor + ```js let sentence = "I enjoy working with JavaScript."; +console.log(sentence); // "I enjoy working with JavaScript." let updatedSentence = sentence.replace("javascript", "coding"); - -console.log(updatedSentence); // Output: "I enjoy working with JavaScript." +console.log(updatedSentence); // "I enjoy working with JavaScript." ``` +::: + Here, since `javascript` (with lowercase `j`) does not match `JavaScript` (with uppercase `J`), the replacement is not made. By default, the `replace()` method will only replace the first occurrence of the `searchValue`. If the value appears multiple times in the string, only the first one will be replaced: +:::interactive_editor + ```js let phrase = "Hello, world! Welcome to the world of coding."; +console.log(phrase); // "Hello, world! Welcome to the world of coding." let updatedPhrase = phrase.replace("world", "universe"); - -console.log(updatedPhrase); // Output: "Hello, universe! Welcome to the world of coding." +console.log(updatedPhrase); // "Hello, universe! Welcome to the world of coding." ``` +::: + Notice that only the first occurrence of `world` is replaced with `universe`. The `replace()` method in JavaScript is a powerful and flexible tool for string manipulation.