Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path028-string-length.js
More file actions
Latest commit
142 lines (108 loc) · 4.25 KB
/
Copy path028-string-length.js
File metadata and controls
142 lines (108 loc) · 4.25 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
130
131
132
133
134
135
136
137
138
139
140
141
142
// 1: STRING LENGTH
constmessage="Hello World";
console.log(message.length);// 11
constpassword="abc123";
if(password.length<8){
console.log("Password too short!");
}
constempty="";
console.log(empty.length);// 0
consttext="Hello\nWorld";
console.log(text.length);// 11 (not 12)
// 2: ACCESSING CHARACTERS
constword="JavaScript";
console.log(word[0]);// "J"
console.log(word[4]);// "S"
console.log(word[9]);// "t"
console.log(word[100]);// undefined
console.log(word.charCodeAt(0));// 74 (Unicode for "J")
// 3: CASE MANIPULATION
// const message = "Hello World";
// console.log(message.toUpperCase()); // "HELLO WORLD"
// console.log(message.toLowerCase()); // "hello world"
// case-insensitive
constuserInput="JAVASCRIPT";
if(userInput.toLowerCase()==="javascript"){
console.log("Match!");
}
constoriginal="JavaScript";
constupper=original.toUpperCase();
console.log(original);// "JavaScript" (unchanged)
console.log(upper);// "JAVASCRIPT"
// 4: TRIMMING WHITESPACE
constmessy=" Hello World ";
console.log(messy.trim());// "Hello World"
console.log(messy.trimStart());// "Hello World "
console.log(messy.trimEnd());// " Hello World"
letusername=" john_doe ";
username=username.trim();
console.log(username);// "john_doe"
// 5: REPLACING TEXT
// const sentence = "I love JavaScript";
// const newSentence = sentence.replace("JavaScript", "coding");
// console.log(newSentence); // "I love coding"
// const text = "cat cat cat";
// console.log(text.replace("cat", "dog")); // "dog cat cat"
// console.log(text.replaceAll("cat", "dog")); // "dog dog dog"
// console.log(text.replace(/cat/g, "dog")); // "dog dog dog"
// const numbers = "1 2 3";
// const doubled = numbers.replace(/\d/g, function(match) {
// return match * 2;
// });
// console.log(doubled); // "2 4 6"
// 6: PADDING STRINGS
constnumber="5";
console.log(number.padStart(3,"0"));// "005"
console.log(number.padEnd(3,"0"));// "500"
constday="7";
constmonth="3";
console.log(`${day.padStart(2,"0")}/${month.padStart(2,"0")}/2025`);// "07/03/2025"
console.log("Hi".padStart(5));// " Hi"
// 7: REPEATING STRINGS
console.log("Ha".repeat(3));// "HaHaHa"
console.log("-".repeat(20));// "--------------------"
conststars="*".repeat(5);
console.log(stars);// "*****"
consttitle="Chapter 1";
console.log(title);
console.log("=".repeat(title.length));
// // Chapter 1
// // =========
// 8: PRACTICAL EXAMPLES
// Example 1: Username Validation
// function validateUsername(username) {
// username = username.trim();
// if (username.length < 3) {
// return "Username too short";
// }
// if (username.length > 20) {
// return "Username too long";
// }
// return "Valid username";
// }
// console.log(validateUsername(" john ")); // "Valid username"
// console.log(validateUsername("ab")); // "Username too short"
// Example 2: Masking Credit Card
functionmaskCard(cardNumber){
constvisibleDigits=4;
constmaskedLength=cardNumber.length-visibleDigits;
constmasked="*".repeat(maskedLength);
constlastFour=cardNumber[cardNumber.length-4]+
cardNumber[cardNumber.length-3]+
cardNumber[cardNumber.length-2]+
cardNumber[cardNumber.length-1];
returnmasked+lastFour;
}
console.log(maskCard("1234567890123456"));// "************3456"
// Example 3: Format Phone Number
functionformatPhone(phone){
constcleaned=phone.replaceAll("-","").replaceAll(" ","");
if(cleaned.length!==10){
return"Invalid phone number";
}
constarea=cleaned[0]+cleaned[1]+cleaned[2];
constprefix=cleaned[3]+cleaned[4]+cleaned[5];
constline=cleaned[6]+cleaned[7]+cleaned[8]+cleaned[9];
return`(${area}) ${prefix}-${line}`;
}
console.log(formatPhone("1234567890"));// "(123) 456-7890"