-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon-string-methods.js
More file actions
129 lines (99 loc) · 4.37 KB
/
Copy pathcommon-string-methods.js
File metadata and controls
129 lines (99 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Practice Problem #1
Activity 1: Searching Strings
Write a program to:
Check if the text "JavaScript" is in the string "Learning JavaScript is fun!" using includes.
Find the position of the word "fun" in the string.
Activity 2: Transforming Strings
Convert the string " CODE BOOTCAMP " to lowercase and remove all extra whitespace.
Replace "BOOTCAMP" with "JavaScript" in the transformed string.
Activity 3: Breaking Apart a Sentence
Split the sentence "Coding is fun and educational" into an array of words.
Activity 4: Retrieving Substrings
Retrieve the first character of "Bootcamp" using charAt.
Extract the word "camp" from "Bootcamp" using slice.
Advanced Challenge
Write a program to process the following string:
Customer: John Doe
Order: Apple, Banana, Grape
Total: $20.50
Extract the customer name.
Split the order into an array of items.
Convert the total price to uppercase (e.g., "TOTAL: $20.50").
Practice Problem #2
Objective
Practice using common string methods to manipulate and extract information from strings.
Instructions:
You are tasked with processing a single string and
performing a series of operations using the string methods covered in the lesson.
Each task corresponds to one or more methods and can be completed independently.
String to Use:
let inputString = " Welcome to the Coding Bootcamp! Learn JavaScript today. ";
Tasks:
Complete the following tasks and assign the results to the specified variables. Log each result to the console.
1. Searching
- Check if the word "JavaScript" is in the string using includes and assign the result to a variable named hasJavaScript.
- Find the position of the word "Coding" in the string using indexOf and assign the result to a variable named codingPosition.
- Check if the string starts with "Welcome" using startsWith and assign the result to a variable named startsWithWelcome.
- Check if the string ends with "today." using endsWith and assign the result to a variable named endsWithToday.
2. Transforming
- Convert the string to all lowercase letters using toLowerCase and assign the result to a variable named lowercaseString.
- Convert the string to all uppercase letters using toUpperCase and assign the result to a variable named uppercaseString.
- Remove the extra spaces from the beginning and end of the string using trim and assign the result to a variable named trimmedString.
- Replace the word "JavaScript" with "coding" using replace and assign the result to a variable named replacedString.
3. Breaking Apart
- 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.
4. Retrieving
- Retrieve the first character of the trimmed string using charAt and assign the result to a variable named firstCharacter.
- Extract the word "Bootcamp" from the string using slice and assign the result to a variable named extractedBootcamp.
*/
//Starter Code
let inputString = " Welcome to the Coding Bootcamp! Learn JavaScript today. ";
// 1. Searching
let hasJavaScript = inputString.includes("JavaScript");
console.log(hasJavaScript);
let codingPosition = inputString.indexOf("Coding");
console.log(codingPosition);
let startsWithWelcome = inputString.trim().startsWith("Welcome");
console.log(startsWithWelcome);
let endsWithToday = inputString.trim().endsWith("today.");
console.log(endsWithToday);
// 2. Transforming
let lowercaseString = inputString.toLowerCase();
console.log(lowercaseString);
let uppercaseString = inputString.toUpperCase();
console.log(uppercaseString);
let trimmedString = inputString.trim();
console.log(trimmedString);
let replacedString = inputString.replace("JavaScript", "coding");
console.log(replacedString);
// 3. Breaking Apart
let wordsArray = inputString.trim().split(" ");
console.log(wordsArray);
// 4. Retrieving
let firstCharacter = trimmedString.charAt(0);
console.log(firstCharacter);
let extractedBootcamp = inputString.slice(inputString.indexOf("Bootcamp"), inputString.indexOf("Bootcamp") + 8);
console.log(extractedBootcamp);
// Log all results
true
16
true
true
welcome to the coding bootcamp! learn javascript today.
WELCOME TO THE CODING BOOTCAMP! LEARN JAVASCRIPT TODAY.
Welcome to the Coding Bootcamp! Learn JavaScript today.
Welcome to the Coding Bootcamp! Learn coding today.
[
'Welcome',
'to',
'the',
'Coding',
'Bootcamp!',
'Learn',
'JavaScript',
'today.'
]
W
Bootcamp
});