@@ -7,17 +7,42 @@ Write a program to:
7
7
Check if the text "JavaScript" is in the string "Learning JavaScript is fun!" using includes.
8
8
Find the position of the word "fun" in the string.
9
9
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
+
10
16
Activity 2: Transforming Strings
11
17
Convert the string " CODE BOOTCAMP " to lowercase and remove all extra whitespace.
12
18
Replace "BOOTCAMP" with "JavaScript" in the transformed string.
13
19
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
+
14
27
Activity 3: Breaking Apart a Sentence
15
28
Split the sentence "Coding is fun and educational" into an array of words.
16
29
30
+ let sentence = "Coding is fun and educational";
31
+ let wordsArray = sentence.split(" ");
32
+ console.log(`Words array: ${wordsArray}`);
33
+
34
+
17
35
Activity 4: Retrieving Substrings
18
36
Retrieve the first character of "Bootcamp" using charAt.
19
37
Extract the word "camp" from "Bootcamp" using slice.
20
38
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
+
21
46
Advanced Challenge
22
47
Write a program to process the following string:
23
48
Customer: John Doe
@@ -69,23 +94,23 @@ Complete the following tasks and assign the results to the specified variables.
69
94
let inputString = " Welcome to the Coding Bootcamp! Learn JavaScript today. " ;
70
95
71
96
// 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." ) ;
76
101
77
102
// 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" ) ;
82
107
83
108
// 3. Breaking Apart
84
- let wordsArray ; // Your code here
109
+ let wordsArray = trimmedString . split ( " " ) ;
85
110
86
111
// 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 )
89
114
90
115
// Log all results
91
116
console . log ( {
0 commit comments