You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//! 2: Using the map method, write a function that takes an array of numbers and returns a new array where each number is squared, but only if it's an even number.
//? Strings in JavaScript are a fundamental data type that represents a sequence of characters.
6
+
7
+
// Note:
8
+
//? Strings created with single or double quotes works the same.
9
+
// There is no difference between the two.
10
+
11
+
//* String Properties:
12
+
//? length: Property that returns the length of the string (number of characters).
13
+
14
+
// const str = "Hello, World!";
15
+
// console.log(str.length);
16
+
// including space n all
17
+
18
+
//* =========================================
19
+
//* Escape Character
20
+
//* =========================================
21
+
22
+
//? Escape Character: In JavaScript, the backslash \ is used as an escape character. It allows you to include special characters in a string.
23
+
24
+
// Code Result Description
25
+
// \' ' Single quote
26
+
// \" " Double quote
27
+
// \\ \ Backslash
28
+
29
+
// let text = "My name is "Vanshika Thesiya " & I am a Full Stack Developer. ";
30
+
31
+
// let text =
32
+
// "My name is ' Vanshika Thesiya ' & \\ I am a \"Full Stack \" Developer. ";
33
+
34
+
// // let text = 'My name is " Vanshika Thesiya " & I am a Full Stack Developer. ';
35
+
36
+
// console.log(text);
37
+
38
+
//* =========================================
39
+
//* String Search Methods
40
+
//* =========================================
41
+
42
+
//? 2: String Search Methods
43
+
//? a: indexOf(): The indexOf() method returns the index (position) of the first occurrence of a string in a string, or it returns -1 if the string is not found:
44
+
// syntax
45
+
// indexOf(searchString)
46
+
// indexOf(searchString, position)
47
+
48
+
// let text = "Vanshika Thesiya";
49
+
// console.log(text.indexOf("Thesiya"));
50
+
// The indexOf() method is case sensitive.
51
+
// console.log(text.indexOf("Thesiya"));
52
+
53
+
// let strArr = Array.from(text);
54
+
// // console.log(strArr);
55
+
// let strMap = strArr.map((curElem, index) => `${curElem} - ${index}`);
56
+
// console.log(strMap);
57
+
58
+
//? b: lastIndexOf() : The lastIndexOf() method returns the index of the last occurrence of a specified text in a string:
59
+
// syntax
60
+
// lastIndexOf(searchString)
61
+
// lastIndexOf(searchString, position)
62
+
63
+
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
64
+
// let index = text.indexOf("JavaScript");
65
+
// let index = text.lastIndexOf("JavaScript");
66
+
// let index = text.lastIndexOf("JavaScript", 40);
67
+
// console.log(index);
68
+
69
+
//? c: search(): The search() method searches a string for a string (or a regular expression) and returns the position of the match.
70
+
//* Returns the index number where the first match is found. Returns -1 if no match is found.
71
+
72
+
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
73
+
// let result = text.search(/Javascript/i);
74
+
// console.log(result);
75
+
76
+
//*👉 Important Tips
77
+
// The search() method cannot take a second start position argument.
78
+
// The indexOf() method cannot take powerful search values (regular expressions).
79
+
// They accept the same arguments (parameters), and return the same value
80
+
81
+
//? match() : Returns an array of the matched values or null if no match is found.
82
+
83
+
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
84
+
// let result = text.match("Javascript");
85
+
// let result = text.match("JavaScript");
86
+
//todo here the js converts the normal text into regular expression text.match(/JavaScript/); without the g flag
87
+
// let result = text.match(/Javascript/gi);
88
+
89
+
// console.log(result);
90
+
91
+
//? matchAll() : Returns an iterator of all matches, providing detailed information about each match. Returns an empty iterator if no match is found.
92
+
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
93
+
// let matchResult = text.matchAll("javascript");
94
+
// let matchResult = text.matchAll("JavaScript");
95
+
//todo here the js converts the normal text into regular expression text.match(/JavaScript/g); also adds the g flag at the end
96
+
97
+
// console.log(...matchResult);
98
+
99
+
// for (let item of matchResult) {
100
+
// console.log(item[0]);
101
+
// }
102
+
103
+
// for (let index of matchResult) {
104
+
// console.log(index.index);
105
+
// }
106
+
107
+
// for (let { index } of matchResult) {
108
+
// console.log(index);
109
+
// }
110
+
111
+
//? includes(): Returns true if the string contains the specified value, and false otherwise.
112
+
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
113
+
// let includeResult = text.includes(/java/i);
114
+
// let includeResult = text.includes("J");
115
+
// console.log(includeResult);
116
+
117
+
// Note: includes() is case sensitive. includes() is an ES6 feature.
118
+
119
+
//? startsWith(): The startsWith() method returns true if a string begins with a specified value.Otherwise it returns false:
120
+
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
121
+
// let result = text.startsWith("Helcome");
122
+
// let result = text.startsWith("Hello");
123
+
// console.log(result);
124
+
125
+
//* start position for the search can be specified
126
+
// let result = text.startsWith("welcome", 18);
127
+
// let result = text.startsWith("welcome", 17);
128
+
// console.log(result);
129
+
130
+
//? endsWith(): The endsWith() method returns true if a string ends with a specified value. Otherwise it returns false:
131
+
132
+
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
133
+
// let result = text.endsWith("welcome");
134
+
// let result = text.endsWith("course");
135
+
// console.log(result);
136
+
137
+
//* =========================================
138
+
//* Extracting String Parts:
139
+
//* =========================================
140
+
//! Extracting String Parts:
141
+
142
+
//? String.prototype.substr() it is deprecated ❌
143
+
144
+
//? a: slice() extracts a part of a string and returns the extracted part in a new string.
145
+
// syntax
146
+
// slice(start, end);
147
+
148
+
// Todo JavaScript counts positions from zero.
149
+
//? First position is 0. Second position is 1.
150
+
151
+
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
152
+
// let result = text.slice(6);
153
+
// let result = text.slice(6, 15);
154
+
// console.log(result);
155
+
156
+
// subString() substring()
157
+
158
+
//? a: substring: Extracts a portion of the string based on starting and ending indices.
159
+
//* camelCase is used to separate words, substring is not to be intended as Sub String but as Substring
160
+
// syntax
161
+
// substring(indexStart) // index starts with 0
162
+
// substring(indexStart, indexEnd)
163
+
164
+
//* substring() is similar to slice(). The difference is that start and end values less than 0 are treated as 0 in substring().
165
+
166
+
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
167
+
// let result = text.slice(-6);
168
+
// console.log(result);
169
+
170
+
//! Homework
171
+
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
172
+
// let result = text.substring(0);
173
+
// let result = text.substring(1);
174
+
// let result = text.substring(-5);
175
+
// console.log(result);
176
+
177
+
//! similarities
178
+
//todo In both the slice() and substring() methods, the end parameter indicates the ending index up to which the extraction occurs, but the character at the end index is excluded from the extracted substring.
179
+
180
+
//* =========================================
181
+
//* Interview Question
182
+
//* =========================================
183
+
//! What is the output for the following code?
184
+
185
+
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
186
+
// let result = text.slice(1);
187
+
// let result = text.replace("H", "");
188
+
// let result = text.substring(1);
189
+
//? Optional
190
+
// let result = text.replace("JavaScript", "Vinod");
191
+
// let result = text.replaceAll("JavaScript", "Vinod");
192
+
193
+
// console.log(result);
194
+
195
+
//* =========================================
196
+
//* Extracting String Characters
197
+
//* =========================================
198
+
//! Extracting String Characters
199
+
// There are 3 methods for extracting string characters:
200
+
201
+
//? The charAt(position) Method
202
+
//? The charCodeAt(position) Method
203
+
//? The at(position) Method
204
+
205
+
//? charAT() : The charAt() method returns the character at a specified index (position) in a string
206
+
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
207
+
// let result = text.charAt(6);
208
+
// let result = text.charAt(-6);
209
+
// console.log(result);
210
+
211
+
//? charCodeAt() : The charCodeAt() method returns the code of the character at a specified index in a string. The method returns a UTF-16 code (an integer between 0 and 65535).
212
+
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
213
+
// let result = text.charCodeAt(6);
214
+
// console.log(result);
215
+
216
+
//todo ES2022 introduced the string method at():
217
+
//? The at() method returns the character at a specified index (position) in a string. The at() method returns the same as carAt().
218
+
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
219
+
// let result = text.at(-6);
220
+
// console.log(result);
221
+
222
+
//todo Note
223
+
// The at() method is a new addition to JavaScript.
224
+
// It allows the use of negative indexes while charAt() do not.
225
+
// Now you can use myString.at(-2) instead of charAt(myString.length-2).
226
+
227
+
//* =========================================
228
+
//* Replacing String Content:
229
+
//* =========================================
230
+
//! Replacing String Content:
231
+
//? replace() : The replace method is used to replace a specified value with another value.
//? trim: Removes whitespace from both ends of the string.
252
+
// const str = " Hello, World! ";
253
+
// console.log(str.length);
254
+
255
+
// let trimStr = str.trim();
256
+
// console.log(trimStr);
257
+
// console.log(trimStr.length);
258
+
259
+
//? split: Splits the string into an array of substrings based on a specified delimiter.
260
+
// const str = "apple,orange,banana";
261
+
// let strArr = str.split(",").reverse().join();
262
+
// console.log(strArr);
263
+
264
+
//* =========================================
265
+
//* //! Interview Questions
266
+
//* =========================================
267
+
268
+
//! 1: Write a JavaScript function that prints the letters 'a' through 'z' in the console. You should use a loop to iterate through the letters and print each one on a new line.
269
+
270
+
// console.log("a".charCodeAt(0));
271
+
// console.log("z".charCodeAt(0));
272
+
273
+
// for (let char = 97; char <= 122; char++) {
274
+
// console.log(String.fromCharCode(char));
275
+
// }
276
+
277
+
//! 2: Write a function to count the number of vowels in a string?
278
+
279
+
// const countVowels = (str) => {
280
+
// const vowels = "aeiou";
281
+
// let count = 0;
282
+
// for (let char of str) {
283
+
// console.log(char);
284
+
// // console.log(str.includes(char));
285
+
// if (vowels.includes(char)) {
286
+
// count++;
287
+
// }
288
+
// }
289
+
// return count;
290
+
// };
291
+
// console.log(checkAllVowelPresentOrNot("my name u is vinod @ thapa"));
292
+
// console.log(countVowels("Hello a i o u world"));
293
+
294
+
//! 3: Write a function to check if all the vowels presents in a string or not?
295
+
296
+
// const checkAllVowelPresentOrNot = (str) => {
297
+
// const vowels = "aeiou";
298
+
// for (let char of vowels) {
299
+
// // console.log(char);
300
+
// // console.log(str.includes(char));
301
+
// if (!str.includes(char)) {
302
+
// return false;
303
+
// }
304
+
// }
305
+
// return true;
306
+
// };
307
+
308
+
// console.log(checkAllVowelPresentOrNot("my name u is vinod @ thapa"));
309
+
310
+
//! 4: Write a JavaScript function to check if the given sting is Pangram or not?
311
+
312
+
// const pangramChecker = (str) => {
313
+
// let inputArr = str.toLowerCase().split("");
314
+
// // console.log(inputArr);
315
+
// // // console.log("z".charCodeAt());
316
+
// const values = inputArr.filter(
317
+
// (curElem) =>
318
+
// curElem.charCodeAt() >= "a".charCodeAt() &&
319
+
// curElem.charCodeAt() <= "z".charCodeAt()
320
+
// );
321
+
// // console.log(values);
322
+
323
+
// return new Set(values).size === 26;
324
+
325
+
// // return [...new Set(values)].length === 26;
326
+
// };
327
+
328
+
// console.log(pangramChecker("The quick @ brown fox jumps ove the lazy dog"));
0 commit comments