fix(curriculum): Add examples to JS string review (#58843)

This commit is contained in:
c0d1ng_ma5ter
2025-02-20 01:55:44 -06:00
committed by GitHub
parent 61893e4655
commit 400011999c
@@ -62,16 +62,84 @@ console.log(char); // A
## Other Common String Methods
- **The `indexOf` Method**: This method is used to search for a substring within a string. If the substring is found, `indexOf` returns the index (or position) of the first occurrence of that substring. If the substring is not found, `indexOf` returns -1, which indicates that the search was unsuccessful.
- **The `includes()` Method**: This method is used to check if a string contains a specific substring. If the substring is found within the string, the method returns true. Otherwise, it returns false.
- **The `slice()` Method**: This method extracts a portion of a string and returns a new string, without modifying the original string. It takes two parameters: the starting index and the optional ending index.
- **The `toUpperCase()` Method**: This method converts all the characters to uppercase letters and returns a new string with all uppercase characters.
- **The `toLowerCase()` Method**: This method converts all characters in a string to lowercase.
- **The `replace()` Method**: This method is used to find a specified value (like a word or character) in a string and replace it with another value.
- **The `repeat()` Method**: This method is used to repeat a string a specified number of times.
- **The `trim()` Method**: This method is used to remove whitespace from both the beginning and the end of a string.
- **The `trimStart()` Method**: This method removes whitespace from the beginning (or "start") of the string.
- **The `trimEnd()` Method**: This method removes whitespace from the end of the string.
- **The `prompt()` Method**: This method is used to get information from a user through the form of a dialog box. This method takes two arguments. The first argument is the message which will appear inside the dialog box, typically prompting the user to enter information. The second one is a default value which is optional and will fill the input field initially.
```js
const text = "The quick brown fox jumps over the lazy dog.";
console.log(text.indexOf("fox")); // 16
console.log(text.indexOf("cat")); // -1
```
- **The `includes()` Method**: This method is used to check if a string contains a specific substring. If the substring is found within the string, the method returns true. Otherwise, it returns false.
```js
const text = "The quick brown fox jumps over the lazy dog.";
console.log(text.includes("fox")); // true
console.log(text.includes("cat")); // false
```
- **The `slice()` Method**: This method extracts a portion of a string and returns a new string, without modifying the original string. It takes two parameters: the starting index and the optional ending index.
```js
const text = "freeCodeCamp";
console.log(text.slice(0,3)); // "free"
console.log(text.slice(4,8)); // "Code"
console.log(text.slice(9,13)); // "Camp"
```
- **The `toUpperCase()` Method**: This method converts all the characters to uppercase letters and returns a new string with all uppercase characters.
```js
const text = "Hello, world!";
console.log(text.toUpperCase()); // "HELLO, WORLD!"
```
- **The `toLowerCase()` Method**: This method converts all characters in a string to lowercase.
```js
const text = "HELLO, WORLD!"
console.log(text.toLowerCase()); // "hello, world!"
```
- **The `replace()` Method**: This method is used to find a specified value (like a word or character) in a string and replace it with another value.
```js
const text = "I like cats";
console.log(text.replace("cats", "dogs")); // "I like dogs"
```
- **The `repeat()` Method**: This method is used to repeat a string a specified number of times.
```js
const text = "Hello";
console.log(text.repeat(3)); // "HelloHelloHello"
```
- **The `trim()` Method**: This method is used to remove whitespaces from both the beginning and the end of a string.
```js
const text = " Hello, world! ";
console.log(text.trim()); // "Hello, world!"
```
- **The `trimStart()` Method**: This method removes whitespaces from the beginning (or "start") of the string.
```js
const text = " Hello, world! ";
console.log(text.trimStart()); // "Hello, world! "
```
- **The `trimEnd()` Method**: This method removes whitespaces from the end of the string.
```js
const text = " Hello, world! ";
console.log(text.trimEnd()); // " Hello, world!"
```
- **The `prompt()` Method**: This method of thw `window` is used to get information from a user through the form of a dialog box. This method takes two arguments. The first argument is the message which will appear inside the dialog box, typically prompting the user to enter information. The second one is a default value which is optional and will fill the input field initially.
```js
const answer = window.prompt("What's your favorite animal?"); // This will change depending on what the user answers
```
# --assignment--