-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
333 lines (274 loc) · 10.9 KB
/
index.html
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<!-- <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JS</title>
</head>
<body> -->
<!-- <form onSubmit="return validateEmail();">
Please enter your last name.<br>
<input type="email" id="address">
<br>
<input type="submit" value="Submit Form">
</form> -->
<!-- <form onSubmit="return validateRadios();">
<input type="radio" name="r1" value="cat"> Cat<br>
<input type="radio" name="r1" value="bat"> Bat<br>
<input type="radio" name="r1" value="hat"> Hat<br>
<input type="submit" value="Submit Form">
</form> -->
<!--
<form onSubmit="return checkPassword();">
Enter a password<br>
(8-12 characters, at least 1 number, no spaces,)<br>
<input type="text" id="f1">
<input type="submit" value="Submit">
</form>
-->
<!-- Explanation
### **1. Changing Case**
Changing the case of strings is a common task when formatting text. JavaScript provides methods to convert strings to uppercase and lowercase.
#### Example 1: Convert to Uppercase
- **Description**: This method converts all letters in the string to uppercase.
```javascript
let text = "hello world";
console.log(text.toUpperCase()); // Output: "HELLO WORLD"
```
#### Example 2: Convert to Lowercase
- **Description**: This method converts all letters in the string to lowercase.
```javascript
let text = "HELLO WORLD";
console.log(text.toLowerCase()); // Output: "hello world"
```
#### Example 3: Mixed Case Conversion
- **Description**: You can apply both methods to convert parts of the text to uppercase or lowercase.
```javascript
let text = "Hello JavaScript!";
console.log(text.toUpperCase()); // Output: "HELLO JAVASCRIPT!"
console.log(text.toLowerCase()); // Output: "hello javascript!"
```
#### Example 4: Change Case for Part of a String
- **Description**: You can combine methods like `slice()` to convert only part of the string to uppercase or lowercase.
```javascript
let text = "hello WORLD";
let newText = text.slice(0, 5).toUpperCase() + text.slice(5).toLowerCase();
console.log(newText); // Output: "HELLO world"
```
#### Example 5: Case-Insensitive String Comparison
- **Description**: To compare two strings regardless of their case, convert both to the same case before comparing.
```javascript
let str1 = "JavaScript";
let str2 = "javascript";
if (str1.toLowerCase() === str2.toLowerCase()) {
console.log("The strings are equal.");
} // Output: "The strings are equal."
```
---
### **2. Strings: Measuring Length and Extracting Parts**
You can measure the length of a string and extract parts of it using letious methods like `length`, `slice()`, `substring()`, and `substr()`.
#### Example 1: Measuring String Length
- **Description**: Use the `.length` property to get the number of characters in a string.
```javascript
let text = "Hello World";
console.log(text.length); // Output: 11
```
#### Example 2: Extracting a Substring using `slice()`
- **Description**: `slice()` extracts a portion of the string starting from one index and ending before another.
```javascript
let text = "JavaScript";
let part = text.slice(0, 4); // Extracts "Java"
console.log(part); // Output: "Java"
```
#### Example 3: Extracting a Substring using `substring()`
- **Description**: `substring()` works similarly to `slice()`, but does not accept negative indices.
```javascript
let text = "JavaScript";
let part = text.substring(4, 10); // Extracts "Script"
console.log(part); // Output: "Script"
```
#### Example 4: Using Negative Index in `slice()`
- **Description**: `slice()` can accept negative values to start extraction from the end of the string.
```javascript
let text = "JavaScript Programming";
let part = text.slice(-11); // Extracts "Programming"
console.log(part); // Output: "Programming"
```
#### Example 5: Extracting with `substr()`
- **Description**: `substr()` extracts a substring starting from an index for a given number of characters.
```javascript
let text = "Hello JavaScript";
let part = text.substr(6, 10); // Extracts 10 characters from index 6
console.log(part); // Output: "JavaScript"
```
---
### **3. Strings: Finding Segments**
You can search for specific parts (segments) of a string using methods like `indexOf()` and `lastIndexOf()`.
#### Example 1: Find the Position of a Word using `indexOf()`
- **Description**: This method returns the index of the first occurrence of the specified substring.
```javascript
let sentence = "Learning JavaScript is fun";
let position = sentence.indexOf("JavaScript");
console.log(position); // Output: 9
```
#### Example 2: Find the Position of a Word That Doesn't Exist
- **Description**: If the substring is not found, `indexOf()` returns `-1`.
```javascript
let sentence = "Learning JavaScript is fun";
let position = sentence.indexOf("Python");
console.log(position); // Output: -1 (not found)
```
#### Example 3: Find the Last Occurrence using `lastIndexOf()`
- **Description**: `lastIndexOf()` searches for the last occurrence of a substring.
```javascript
let sentence = "JavaScript is great, and JavaScript is fun";
let lastPosition = sentence.lastIndexOf("JavaScript");
console.log(lastPosition); // Output: 27
```
#### Example 4: Find Position from a Specific Index
- **Description**: You can specify a starting point for `indexOf()` to begin the search.
```javascript
let sentence = "JavaScript is great, and JavaScript is fun";
let position = sentence.indexOf("JavaScript", 10); // Start searching after index 10
console.log(position); // Output: 27
```
#### Example 5: Case-Sensitive Search
- **Description**: `indexOf()` is case-sensitive, so it will not match "JavaScript" with "javascript".
```javascript
let sentence = "Learning JavaScript is Fun";
let position = sentence.indexOf("javascript"); // Case-sensitive
console.log(position); // Output: -1 (not found)
```
---
### **4. Strings: Finding a Character at a Location**
You can find a specific character in a string by its position using `charAt()` or bracket notation.
#### Example 1: Finding a Character Using `charAt()`
- **Description**: The `charAt()` method returns the character at a specific index.
```javascript
let text = "JavaScript";
console.log(text.charAt(4)); // Output: "S"
```
#### Example 2: Accessing a Character Directly
- **Description**: You can also use bracket notation to access a character by index.
```javascript
let text = "JavaScript";
console.log(text[4]); // Output: "S"
```
#### Example 3: Finding Character at the Last Position
- **Description**: Use `.length - 1` to find the last character of a string.
```javascript
let text = "JavaScript";
console.log(text.charAt(text.length - 1)); // Output: "t"
```
#### Example 4: Get Unicode Value of a Character using `charCodeAt()`
- **Description**: `charCodeAt()` returns the Unicode value of the character at a given index.
```javascript
let text = "A";
console.log(text.charCodeAt(0)); // Output: 65 (Unicode of "A")
```
#### Example 5: Check if a Character Exists at a Specific Index
- **Description**: Check if there's a character at a specific index in a string.
```javascript
let text = "Hello World";
if (text[15]) { console.log("Character exists at index 15.");
} else { console.log("No character at index 15."); // Output: "No character at index 15."
}
```
---
### **5. Strings: Replacing Characters**
You can replace parts of a string using the `replace()` method, either for the first occurrence or globally.
#### Example 1: Simple String Replacement using `replace()`
- **Description**: Replace a substring with another string.
```javascript
let sentence = "I like apples";
let newSentence = sentence.replace("apples", "bananas");
console.log(newSentence); // Output: "I like bananas"
```
#### Example 2: Replacing Only the First Occurrence
- **Description**: `replace()` replaces only the first occurrence of the substring.
```javascript
let sentence = "Apples are apples";
let newSentence = sentence.replace("apples", "bananas");
console.log(newSentence); // Output: "Apples are bananas"
```
#### Example 3: Using Regular Expressions for Case-Insensitive Replacement
- **Description**: Use a regular expression with the `i` flag for case-insensitive replacement.
```javascript
let sentence = "Apples are Apples";
let newSentence = sentence.replace(/apples/i, "bananas");
console.log(newSentence); // Output: "bananas are Apples"
```
#### Example 4: Replacing All Occurrences Using Regex
- **Description**: The `g` flag in a regular expression replaces all occurrences in the string.
```javascript
let sentence = "Apples are apples";
let newSentence = sentence.replace(/apples/g, "bananas");
console.log(newSentence); // Output: "bananas are bananas"
```
#### Example 5: Replacing Multiple Words Using `replace()`
- **Description**: You can chain `replace()` methods to replace multiple different words.
```javascript
let sentence = "I love cats and dogs";
let newSentence = sentence.replace("cats", "birds").replace("dogs", "fish");
console.log(newSentence); // Output: "I love birds and fish" -->
<!-- <script src="main.js"></script> -->
<!-- <script>
let i = 0;
while (i < 0) {
alert(i);
i++;
}
</script> -->
<!-- <a href="#" onClick="alert('Hi');">Click</a> -->
<!-- <a href="JavaScript:void(0)" onClick="popup('Hi');">Click</a> -->
<!-- <a href="JavaScript:void(0)" onClick="let greet = hi; alert(greet);">Click</a> -->
<!-- <input type="button" value="Click" onClick="alert('Hello world!');"> -->
<!--
</body>
</html> -->
<!-- // function outer() {
// let message = "Hello";
// return function inner() {
// console.log(message); // Output: Hello
// };
// }
// console.log(message);
// const greet = outer();
// greet();
// console.log(number);
// function createCounter() {
// let count = 0;
// return function () {
// count++;
// return count;
// };
// }
// const counter = createCounter();
// console.log(counter()); // Output: 1
// console.log(counter()); // Output: 2
// function createSecret(secret) {
// return function () {
// console.log(secret);
// };
// }
// const secretMessage = createSecret("This is a secret!");
// const [a = 1, b = 2] = []
// console.log(a-b);
// function greet(name = "Guest") {
// console.log(`Hello, ${name}!`);
// }
// greet(); // Output: Hello, Guest!
// greet(prompt("Enter Your Name")); // Output: Hello, Ali!
// function add(a = 5, b = 10,) {
// return a + b;
// }
// // console.log(add()); // Output: 15
// // console.log(add(7)); // Output: 17
// console.log(add(3, 4, 5)); // Output: 7
// const arr1 = [1, 2, 3];
// const arr2 = [...arr1, 4, 5];
// console.log(arr2); // Output: [1, 2, 3, 4, 5]
// const person = { name: "Ali"};
// const newPerson = { ...person,mail: "@gmail.com" };
// console.log(newPerson.name+newPerson.mail); // Output: { name: 'Ali', age: 25, country: 'Pakistan' }
-->