Skip to content

Commit 2d60a2b

Browse files
committed
practice string methods
1 parent ce4948b commit 2d60a2b

File tree

1 file changed

+53
-97
lines changed

1 file changed

+53
-97
lines changed

common-string-methods.js

Lines changed: 53 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,59 @@
1-
/*
2-
3-
Practice Problem #1
4-
5-
Activity 1: Searching Strings
6-
Write a program to:
7-
Check if the text "JavaScript" is in the string "Learning JavaScript is fun!" using includes.
8-
Find the position of the word "fun" in the string.
9-
10-
Activity 2: Transforming Strings
11-
Convert the string " CODE BOOTCAMP " to lowercase and remove all extra whitespace.
12-
Replace "BOOTCAMP" with "JavaScript" in the transformed string.
13-
14-
Activity 3: Breaking Apart a Sentence
15-
Split the sentence "Coding is fun and educational" into an array of words.
16-
17-
Activity 4: Retrieving Substrings
18-
Retrieve the first character of "Bootcamp" using charAt.
19-
Extract the word "camp" from "Bootcamp" using slice.
20-
21-
Advanced Challenge
22-
Write a program to process the following string:
23-
Customer: John Doe
24-
Order: Apple, Banana, Grape
25-
Total: $20.50
26-
Extract the customer name.
27-
Split the order into an array of items.
28-
Convert the total price to uppercase (e.g., "TOTAL: $20.50").
29-
30-
31-
Practice Problem #2
32-
33-
Objective
34-
Practice using common string methods to manipulate and extract information from strings.
35-
36-
Instructions:
37-
You are tasked with processing a single string and
38-
performing a series of operations using the string methods covered in the lesson.
39-
Each task corresponds to one or more methods and can be completed independently.
40-
41-
String to Use:
42-
let inputString = " Welcome to the Coding Bootcamp! Learn JavaScript today. ";
43-
44-
Tasks:
45-
Complete the following tasks and assign the results to the specified variables. Log each result to the console.
46-
47-
1. Searching
48-
- Check if the word "JavaScript" is in the string using includes and assign the result to a variable named hasJavaScript.
49-
- Find the position of the word "Coding" in the string using indexOf and assign the result to a variable named codingPosition.
50-
- Check if the string starts with "Welcome" using startsWith and assign the result to a variable named startsWithWelcome.
51-
- Check if the string ends with "today." using endsWith and assign the result to a variable named endsWithToday.
52-
53-
2. Transforming
54-
- Convert the string to all lowercase letters using toLowerCase and assign the result to a variable named lowercaseString.
55-
- Convert the string to all uppercase letters using toUpperCase and assign the result to a variable named uppercaseString.
56-
- Remove the extra spaces from the beginning and end of the string using trim and assign the result to a variable named trimmedString.
57-
- Replace the word "JavaScript" with "coding" using replace and assign the result to a variable named replacedString.
58-
59-
3. Breaking Apart
60-
- Split the string into an array of words using split with a space (" ") as the delimiter and assign the result to a variable named wordsArray.
61-
62-
4. Retrieving
63-
- Retrieve the first character of the trimmed string using charAt and assign the result to a variable named firstCharacter.
64-
- Extract the word "Bootcamp" from the string using slice and assign the result to a variable named extractedBootcamp.
65-
66-
*/
67-
68-
//Starter Code
1+
//String to Use
692
let inputString = " Welcome to the Coding Bootcamp! Learn JavaScript today. ";
703

71-
// 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
76-
77-
// 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
4+
// 1.1 Searching
5+
let hasJavaScript = inputString.includes("JavaScript");
6+
if(hasJavaScript) {
7+
console.log("Provided input string contains word JavaScript")
8+
} else{
9+
console.log("Provided input string doesn't contains word JavaScript")
10+
}
11+
12+
//1.2 Finding the indexOf of a word "Coding"
13+
let codingPosition = inputString.indexOf("Coding");
14+
console.log("Index of the word coding in the provided input string is: " + codingPosition);
15+
16+
// 1.3 Checking if the string starts with word "Welcome"
17+
let startsWithWelcome = inputString.startsWith("Welcome");
18+
if(startsWithWelcome) {
19+
console.log("Provided input string starts with word Welcome");
20+
} else{
21+
console.log("Provided input string doesn't starts with word Welcome");
22+
}
23+
24+
//1.4 checking
25+
let endsWithToday = inputString.endsWith("today.");
26+
if(endsWithToday) {
27+
console.log("Provided input string ends with word today.");
28+
} else{
29+
console.log("Provided input string doesn't end with word today.");
30+
}
31+
32+
// 2.1 lower case
33+
let lowercaseString = inputString.toLowerCase();
34+
console.log("Provided input string in lower case: " + lowercaseString);
35+
36+
// 2.2 upper case
37+
let uppercaseString = inputString.toUpperCase();
38+
console.log("Provided input string in upper case: " + uppercaseString);
39+
40+
//2.3 trimming extra spaces
41+
let trimmedString = inputString.trim(" ");
42+
console.log("Provided input string after trimming the extra spaces: " + trimmedString);
43+
44+
//2.4 replace
45+
let replacedString = inputString.replace("JavaScript","coding");
46+
console.log("Provided input string after replacing the JavaScript word with coding: " + replacedString);
8247

8348
// 3. Breaking Apart
84-
let wordsArray; // Your code here
49+
let wordsArray = inputString.split(" ");
50+
console.log("Provided input string after breaking apart: ");
51+
console.log(wordsArray);
8552

86-
// 4. Retrieving
87-
let firstCharacter; // Your code here
88-
let extractedBootcamp; // Your code here
53+
// 4.1 Retrieving character of the trimmed string
54+
let firstCharacter = inputString.trim(" ").charAt(0);
55+
console.log("first character of the trimmed input string is: " + firstCharacter);
8956

90-
// Log all results
91-
console.log({
92-
hasJavaScript,
93-
codingPosition,
94-
startsWithWelcome,
95-
endsWithToday,
96-
lowercaseString,
97-
uppercaseString,
98-
trimmedString,
99-
replacedString,
100-
wordsArray,
101-
firstCharacter,
102-
extractedBootcamp,
103-
});
57+
//4.2 Extracting the word Bootcamp from the input string
58+
let extractedBootcamp = inputString.slice(24,32);
59+
console.log("Extracted word from the provided input string is: " + extractedBootcamp);

0 commit comments

Comments
 (0)