From 06c1a1fb4945047d0e00eee8e9821d68f12766dc Mon Sep 17 00:00:00 2001 From: Clarence Bakosi Date: Tue, 28 Oct 2025 23:07:35 +0100 Subject: [PATCH] feat(curriculum): Add interactive examples to What Is Bracket Notation, and How Do You Access Characters from a String (#63189) --- .../672d2654f78cbf20e0ba4501.md | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/curriculum/challenges/english/blocks/lecture-working-with-strings-in-javascript/672d2654f78cbf20e0ba4501.md b/curriculum/challenges/english/blocks/lecture-working-with-strings-in-javascript/672d2654f78cbf20e0ba4501.md index b870be7ea60..3a4945b5858 100644 --- a/curriculum/challenges/english/blocks/lecture-working-with-strings-in-javascript/672d2654f78cbf20e0ba4501.md +++ b/curriculum/challenges/english/blocks/lecture-working-with-strings-in-javascript/672d2654f78cbf20e0ba4501.md @@ -5,7 +5,7 @@ challengeType: 19 dashedName: what-is-bracket-notation-and-how-do-you-access-characters-from-a-string --- -# --description-- +# --interactive-- In JavaScript, strings are treated as sequences of characters, and each character in a string can be accessed using bracket notation. This allows you to retrieve a specific character from a string based on its position, which is called its index. @@ -15,31 +15,43 @@ For example, in the string `hello`, the character `h` is at index `0`, `e` is at Bracket notation uses square brackets (`[]`) and the index of the character you want to access. Let’s look at an example: +:::interactive_editor + ```js let greeting = "hello"; -console.log(greeting[1]); // Output: "e" +console.log(greeting[1]); // "e" ``` +::: + In this example, we can access the character at index `1`, which is `e`. To get the last character of a string, you can use the length of the string minus one. The `length` property of a string tells you how many characters it contains, so to access the last character, you would subtract one from the length: +:::interactive_editor + ```js let greeting = "hello"; -console.log(greeting[greeting.length - 1]); // Output: "o" +console.log(greeting[greeting.length - 1]); // "o" ``` +::: + In this case, the `length` of `hello` is `5`, and the last character (`o`) is at index `4` which is `5 - 1`. If you want to get multiple characters, you can use bracket notation like this: +:::interactive_editor + ```js let greeting = "hello"; -let firstTwo = greeting[0] + greeting[1]; // Output: "he" +let firstTwo = greeting[0] + greeting[1]; // "he" console.log(firstTwo); ``` +::: + In this example, we are concatenating the first and second characters using bracket notation to form the string `he`. Bracket notation is useful when you need to access specific characters in a string, such as extracting initials from a name or checking a specific letter for validation.