Skip to content

Commit 5adf8a6

Browse files
committed
Add initial code
1 parent ce4948b commit 5adf8a6

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

common-string-methods.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,107 @@ console.log({
101101
firstCharacter,
102102
extractedBootcamp,
103103
});
104+
105+
// Practice Problem 1
106+
107+
// Activity 1. Searching Strings
108+
109+
// Check if "JavaScript" is in the string
110+
let str = "Learning JavaScript is fun!";
111+
console.log(str.includes("JavaScript")); // true
112+
113+
// Find the position of "fun"
114+
console.log(str.indexOf("fun")); // 22
115+
116+
117+
// Activity 2. Transforming Strings
118+
119+
// Transform the string
120+
let codeStr = " CODE BOOTCAMP ";
121+
let transformedStr = codeStr.trim().toLowerCase().replace("bootcamp", "JavaScript");
122+
console.log(transformedStr); // "code javascript"
123+
124+
125+
// Activity 3. Breaking Apart a Sentence
126+
127+
// Split the sentence into an array of words
128+
let sentence = "Coding is fun and educational";
129+
let wordsArray = sentence.split(" ");
130+
console.log(wordsArray); // ["Coding", "is", "fun", "and", "educational"]
131+
132+
133+
// Activity 4. Retrieving Substrings
134+
135+
// Retrieve the first character of "Bootcamp"
136+
let word = "Bootcamp";
137+
console.log(word.charAt(0)); // "B"
138+
139+
// Extract the word "camp" from "Bootcamp"
140+
console.log(word.slice(4)); // "camp"
141+
142+
143+
// Advanced Challenge
144+
145+
let customerInfo = `
146+
Customer: John Doe
147+
Order: Apple, Banana, Grape
148+
Total: $20.50
149+
`;
150+
151+
// Extract the customer name
152+
let customerName = customerInfo.match(/Customer: (.+)/)[1];
153+
console.log(customerName); // "John Doe"
154+
155+
// Split the order into an array of items
156+
let orderArray = customerInfo.match(/Order: (.+)/)[1].split(", ");
157+
console.log(orderArray); // ["Apple", "Banana", "Grape"]
158+
159+
// Convert the total price to uppercase
160+
let total = customerInfo.match(/Total: (.+)/)[1];
161+
console.log(`TOTAL: ${total}`); // "TOTAL: $20.50"
162+
163+
164+
// Practice Problem 2
165+
166+
// Task 1. Searching
167+
168+
let hasJavaScript = inputString.includes("JavaScript"); // Check if "JavaScript" is in the string
169+
let codingPosition = inputString.indexOf("Coding"); // Find the position of "Coding"
170+
let startsWithWelcome = inputString.trim().startsWith("Welcome"); // Check if it starts with "Welcome"
171+
let endsWithToday = inputString.trim().endsWith("today."); // Check if it ends with "today."
172+
173+
174+
// Task 2. Transforming
175+
176+
let lowercaseString = inputString.toLowerCase(); // Convert to lowercase
177+
let uppercaseString = inputString.toUpperCase(); // Convert to uppercase
178+
let trimmedString = inputString.trim(); // Remove extra spaces
179+
let replacedString = inputString.replace("JavaScript", "coding"); // Replace "JavaScript" with "coding"
180+
181+
182+
// Task 3. Breaking Apart
183+
184+
let wordsArray = inputString.trim().split(" "); // Split into an array of words
185+
186+
187+
// Task 4. Retrieving
188+
189+
let firstCharacter = inputString.trim().charAt(0); // Retrieve the first character of the trimmed string
190+
let extractedBootcamp = inputString.slice(inputString.indexOf("Bootcamp"), inputString.indexOf("Bootcamp") + 8); // Extract "Bootcamp"
191+
192+
193+
// Log all results
194+
195+
console.log({
196+
hasJavaScript,
197+
codingPosition,
198+
startsWithWelcome,
199+
endsWithToday,
200+
lowercaseString,
201+
uppercaseString,
202+
trimmedString,
203+
replacedString,
204+
wordsArray,
205+
firstCharacter,
206+
extractedBootcamp,
207+
});

0 commit comments

Comments
 (0)