|
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 |
| -
|
| 1 | +// Starter Code |
| 2 | +let inputString = " Welcome to the Coding Bootcamp! Learn JavaScript today. "; |
30 | 3 |
|
31 |
| -Practice Problem #2 |
| 4 | +// 1. Searching |
32 | 5 |
|
33 |
| -Objective |
34 |
| -Practice using common string methods to manipulate and extract information from strings. |
| 6 | +// Check if the word "JavaScript" is in the string using includes |
| 7 | +let hasJavaScript = inputString.includes("JavaScript"); |
| 8 | +// Expected output: true |
35 | 9 |
|
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. |
| 10 | +// Find the position of the word "Coding" in the string using indexOf |
| 11 | +let codingPosition = inputString.indexOf("Coding"); |
| 12 | +// Expected output: 15 |
40 | 13 |
|
41 |
| -String to Use: |
42 |
| - let inputString = " Welcome to the Coding Bootcamp! Learn JavaScript today. "; |
| 14 | +// Check if the string starts with "Welcome" using startsWith |
| 15 | +let startsWithWelcome = inputString.trim().startsWith("Welcome"); |
| 16 | +// Expected output: true |
43 | 17 |
|
44 |
| -Tasks: |
45 |
| -Complete the following tasks and assign the results to the specified variables. Log each result to the console. |
| 18 | +// Check if the string ends with "today." using endsWith |
| 19 | +let endsWithToday = inputString.trim().endsWith("today."); |
| 20 | +// Expected output: true |
46 | 21 |
|
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. |
| 22 | +// 2. Transforming |
52 | 23 |
|
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. |
| 24 | +// Convert the string to all lowercase letters using toLowerCase |
| 25 | +let lowercaseString = inputString.toLowerCase(); |
| 26 | +// Expected output: " welcome to the coding bootcamp! learn javascript today. " |
58 | 27 |
|
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. |
| 28 | +// Convert the string to all uppercase letters using toUpperCase |
| 29 | +let uppercaseString = inputString.toUpperCase(); |
| 30 | +// Expected output: " WELCOME TO THE CODING BOOTCAMP! LEARN JAVASCRIPT TODAY. " |
61 | 31 |
|
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. |
| 32 | +// Remove the extra spaces from the beginning and end of the string using trim |
| 33 | +let trimmedString = inputString.trim(); |
| 34 | +// Expected output: "Welcome to the Coding Bootcamp! Learn JavaScript today." |
65 | 35 |
|
66 |
| -*/ |
| 36 | +// Replace the word "JavaScript" with "coding" using replace |
| 37 | +let replacedString = inputString.replace("JavaScript", "coding"); |
| 38 | +// Expected output: " Welcome to the Coding Bootcamp! Learn coding today. " |
67 | 39 |
|
68 |
| -//Starter Code |
69 |
| -let inputString = " Welcome to the Coding Bootcamp! Learn JavaScript today. "; |
| 40 | +// 3. Breaking Apart |
70 | 41 |
|
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 |
| 42 | +// Split the string into an array of words using split with a space (" ") as the delimiter |
| 43 | +let wordsArray = inputString.trim().split(" "); |
| 44 | +// Expected output: ["Welcome", "to", "the", "Coding", "Bootcamp!", "Learn", "JavaScript", "today."] |
76 | 45 |
|
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 |
| 46 | +// 4. Retrieving |
82 | 47 |
|
83 |
| -// 3. Breaking Apart |
84 |
| -let wordsArray; // Your code here |
| 48 | +// Retrieve the first character of the trimmed string using charAt |
| 49 | +let firstCharacter = trimmedString.charAt(0); |
| 50 | +// Expected output: "W" |
85 | 51 |
|
86 |
| -// 4. Retrieving |
87 |
| -let firstCharacter; // Your code here |
88 |
| -let extractedBootcamp; // Your code here |
| 52 | +// Extract the word "Bootcamp" from the string using slice |
| 53 | +let extractedBootcamp = trimmedString.slice(19, 27); |
| 54 | +// Expected output: "Bootcamp" |
89 | 55 |
|
90 | 56 | // Log all results
|
91 | 57 | console.log({
|
|
0 commit comments