Skip to content

Commit e09c831

Browse files
committed
Common string methods
1 parent ce4948b commit e09c831

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

common-string-methods.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,42 @@ Write a program to:
77
Check if the text "JavaScript" is in the string "Learning JavaScript is fun!" using includes.
88
Find the position of the word "fun" in the string.
99
10+
let string1 = "Learning JavaScript is fun!";
11+
let hasJavaScript = string1.includes("JavaScript");
12+
let funPosition = string1.indexOf("fun");
13+
console.log(`Contains "JavaScript": ${hasJavaScript}`);
14+
console.log(`Position of "fun": ${funPosition}`);
15+
1016
Activity 2: Transforming Strings
1117
Convert the string " CODE BOOTCAMP " to lowercase and remove all extra whitespace.
1218
Replace "BOOTCAMP" with "JavaScript" in the transformed string.
1319
20+
let string2 = " CODE BOOTCAMP ";
21+
let lowercaseString = string2.toLowerCase().trim();
22+
let replacedString = lowercaseString.replace("bootcamp", "javascript");
23+
console.log(`Lowercase and trimmed: "${lowercaseString}"`);
24+
console.log(`Replaced string: "${replacedString}"`);
25+
26+
1427
Activity 3: Breaking Apart a Sentence
1528
Split the sentence "Coding is fun and educational" into an array of words.
1629
30+
let sentence = "Coding is fun and educational";
31+
let wordsArray = sentence.split(" ");
32+
console.log(`Words array: ${wordsArray}`);
33+
34+
1735
Activity 4: Retrieving Substrings
1836
Retrieve the first character of "Bootcamp" using charAt.
1937
Extract the word "camp" from "Bootcamp" using slice.
2038
39+
let word = "Bootcamp";
40+
let firstCharacter = word.charAt(0);
41+
let extractedCamp = word.slice(4); // "camp"
42+
console.log(`First character: ${firstCharacter}`);
43+
console.log(`Extracted 'camp': ${extractedCamp}`);
44+
45+
2146
Advanced Challenge
2247
Write a program to process the following string:
2348
Customer: John Doe
@@ -69,23 +94,23 @@ Complete the following tasks and assign the results to the specified variables.
6994
let inputString = " Welcome to the Coding Bootcamp! Learn JavaScript today. ";
7095

7196
// 1. Searching
72-
let hasJavaScript; // Your code here
73-
let codingPosition; // Your code here
74-
let startsWithWelcome; // Your code here
75-
let endsWithToday; // Your code here
97+
let hasJavaScript = inputString.includes("JavaScript");
98+
let codingPosition = inputString.indexOf("Coding");
99+
let startsWithWelcome = inputString.trim().startsWith("Welcome");
100+
let endsWithToday = inputString.trim().endsWith("today.");
76101

77102
// 2. Transforming
78-
let lowercaseString; // Your code here
79-
let uppercaseString; // Your code here
80-
let trimmedString; // Your code here
81-
let replacedString; // Your code here
103+
let lowercaseString = inputString.toLowerCase();
104+
let uppercaseString = inputString.toUpperCase();
105+
let trimmedString = inputString.trim();
106+
let replacedString = inputString.replace("JavaScript", "coding");
82107

83108
// 3. Breaking Apart
84-
let wordsArray; // Your code here
109+
let wordsArray = trimmedString.split(" ");
85110

86111
// 4. Retrieving
87-
let firstCharacter; // Your code here
88-
let extractedBootcamp; // Your code here
112+
let firstCharacter = trimmedString.charAt(0);
113+
let extractedBootcamp = inputString.slice(trimmedString.indexOf("Bootcamp"), trimmedString.indexOf("Bootcamp") + 8)
89114

90115
// Log all results
91116
console.log({

0 commit comments

Comments
 (0)