Skip to content

Commit 1e69a3e

Browse files
Completed string methods practice
1 parent ce4948b commit 1e69a3e

File tree

1 file changed

+12
-79
lines changed

1 file changed

+12
-79
lines changed

common-string-methods.js

Lines changed: 12 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,24 @@
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+
// Starter Code
692
let inputString = " Welcome to the Coding Bootcamp! Learn JavaScript today. ";
703

714
// 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
5+
let hasJavaScript = inputString.includes("JavaScript"); // Check if "JavaScript" is in the string
6+
let codingPosition = inputString.indexOf("Coding"); // Find the position of "Coding"
7+
let startsWithWelcome = inputString.trim().startsWith("Welcome"); // Check if it starts with "Welcome"
8+
let endsWithToday = inputString.trim().endsWith("today."); // Check if it ends with "today."
769

7710
// 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
11+
let lowercaseString = inputString.toLowerCase(); // Convert to lowercase
12+
let uppercaseString = inputString.toUpperCase(); // Convert to uppercase
13+
let trimmedString = inputString.trim(); // Remove extra spaces
14+
let replacedString = inputString.replace("JavaScript", "coding"); // Replace "JavaScript" with "coding"
8215

8316
// 3. Breaking Apart
84-
let wordsArray; // Your code here
17+
let wordsArray = trimmedString.split(" "); // Split string into words
8518

8619
// 4. Retrieving
87-
let firstCharacter; // Your code here
88-
let extractedBootcamp; // Your code here
20+
let firstCharacter = trimmedString.charAt(0); // Get the first character of the trimmed string
21+
let extractedBootcamp = trimmedString.slice(trimmedString.indexOf("Bootcamp"), trimmedString.indexOf("Bootcamp") + 8); // Extract "Bootcamp"
8922

9023
// Log all results
9124
console.log({

0 commit comments

Comments
 (0)