-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
369 lines (320 loc) · 11.8 KB
/
app.js
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
document.write("<br>");
document.write("<h3>Question no 01</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques1() ">Click Me</button>');
document.write("<br>");
document.write("<br>");
function ques1() {
var firstName = prompt("Enter your first name:");
var lastName = prompt("Enter your last name:");
var fullName = firstName + " " + lastName;
alert("Hello, " + fullName + "! Welcome.");
console.log("Hello, " + fullName + "! Welcome.");
}
// question no 2
document.write("<h3>Question no 02</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques2();">Click Me</button>');
function ques2() {
var favoriteModel = prompt("What is your favorite mobile phone model?");
var inputLength = favoriteModel.length;
alert("The length of your favorite mobile phone model input is: " + inputLength);
console.log("The length of your favorite mobile phone model input is: " + inputLength);
}
document.write("<br>");
document.write("<br>");
//question no 3
document.write("<h3>Question no 03</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques3()">Click Me</button>');
function ques3() {
var word = "Pakistani";
var indexOfN = word.indexOf("n");
alert("The index of letter 'n' in the word 'Pakistani' is: " + indexOfN);
console.log("The index of letter 'n' in the word 'Pakistani' is: " + indexOfN);
}
document.write("<br>");
document.write("<br>");
//question no 4
document.write("<h3>Question no 04</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques4()">Click Me</button>');
function ques4() {
var word = "Hello World";
var lastIndexOfL = word.lastIndexOf("l");
alert("The last index of letter 'l' in the word 'Hello World' is: " + lastIndexOfL);
console.log("The last index of letter 'l' in the word 'Hello World' is: " + lastIndexOfL);
};
document.write("<br>");
document.write("<br>");
//question no 5
document.write("<h3>Question no 05</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques5()">Click Me</button>');
function ques5() {
var word = "Pakistani";
var characterAtIndex3 = word.charAt(3);
console.log("The character at the 3rd index in the word 'Pakistani' is: " + characterAtIndex3);
alert("The character at the 3rd index in the word 'Pakistani' is: " + characterAtIndex3);
}
// document.write('<div id="pere"></div>')
document.write("<br>");
document.write("<br>");
//question no 6
document.write("<h3>Question no 06</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques6()"">Click Me</button>');
function ques6() {
var firstName = prompt("Enter your first name:");
var lastName = prompt("Enter your last name:");
var fullName = firstName.concat(" ", lastName);
alert("Hello, " + fullName + "! Welcome.");
}
// document.write('<div id="result"></div>')
document.write("<br>");
document.write("<br>");
//question no 7
document.write("<h3>Question no 07</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques7()">Click Me</button>');
function ques7() {
var word = "Hyderabad";
var replacedWord = word.replace("Hyder", "Islam");
alert("The replaced word is: " + replacedWord);
console.log("The replaced word is: " + replacedWord);
}
document.write("<br>");
document.write("<br>");
//question no 8
document.write("<h3>Question no 08</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques8()">Click Me</button>');
function ques8() {
var message = "Ali and Sami are best friends. They play cricket and football together.";
var replacedMessage = message.split("and").join("&");
console.log("The modified message is: " + replacedMessage);
alert("The modified message is: " + replacedMessage);
}
document.write("<br>");
document.write("<br>");
//question no 9
document.write("<h3>Question no 09</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques9()">Click Me</button>');
function ques9() {
var str = "472";
var num = parseInt(str);
console.log("Value: " + num + "\r" + "Type: " + typeof (num));
alert("Value: " + num + "\r" + "Type: " + typeof (num));
}
document.write("<br>");
document.write("<br>");
//question no 10
document.write("<h3>Question no 10</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques10()">Click Me</button>');
function ques10() {
var userInput = prompt("Enter your input:");
var capitalizedInput = userInput.toUpperCase();
alert("Use Input: " + userInput + '\r' + 'Upper Case :' + capitalizedInput);
console.log("Use Input: " + userInput + '\r' + 'Upper Case :' + capitalizedInput);
}
document.write("<br>");
document.write("<br>");
//question no 11
document.write("<h3>Question no 11</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques11()">Click Me</button>');
function ques11() {
var userInput = prompt("Enter your input:");
var words = userInput.toLowerCase().split(" ");
for (var i = 0; i < words.length; i++) {
words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
}
var titleCaseInput = words.join(" ");
alert("Input in title case: " + titleCaseInput);
console.log("Input in title case: " + titleCaseInput);
}
document.write("<br>");
document.write("<br>");
//question no 12
document.write("<h3>Question no 12</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques12()">Click Me</button>');
function ques12() {
var num = prompt('For Example\r' + 35.36 + '\r Pu know');
var numAsString = num.toString();
var stringWithoutDot = numAsString.replace(".", "");
alert("Modified string without dot: " + stringWithoutDot);
console.log("Modified string without dot: " + stringWithoutDot);
}
document.write("<br>");
document.write("<br>");
//question no 13
document.write("<h3>Question no 13</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques13()">Click Me</button>');
function ques13() {
var username = prompt("Enter your username:");
var specialSymbols = ['@', '.', ',', '!'];
function isSpecialSymbol(char) {
var charCode = char.charCodeAt(0);
return specialSymbols.includes(char);
}
function containsSpecialSymbols(username) {
for (var i = 0; i < username.length; i++) {
if (isSpecialSymbol(username[i])) {
return true;
}
}
return false;
}
if (containsSpecialSymbols(username)) {
// Prompt the user to enter a valid usernam
alert("Please enter a valid username without special symbols [@ . , !]");
} else {
// Username is valid, display it
alert("Your username is: " + username);
}
}
document.write("<br>");
document.write("<br>");
//question no 14
document.write("<h3>Question no 14</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques14()">Click Me</button>');
function ques14() {
var A = ["cake", "apple pie", "cookie", "chips", "patties"];
var userInput = prompt("Enter an item to search:").toLowerCase();
var found = false;
for (var i = 0; i < A.length; i++) {
if (A[i].toLowerCase() === userInput) {
found = true;
break;
}
}
if (found) {
alert("Yes, the item '" + userInput + "' is found in the list.");
} else {
alert("No, the item '" + userInput + "' is not found in the list.");
}
}
document.write("<br>");
document.write("<br>");
//question no 15
document.write("<h3>Question no 15</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques15()">Click Me</button>');
function ques15() {
function isLetter(char) {
var charCode = char.charCodeAt(0);
return (charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122);
}
function isDigit(char) {
var charCode = char.charCodeAt(0);
return charCode >= 48 && charCode <= 57;
}
// Prompt the user to enter a password
var password = prompt("Enter your password:");
// Check if the password meets the requirements
if (
password.length < 6 || // Check length
isDigit(password[0]) || // Check if it starts with a number
!/[a-zA-Z]/.test(password) || // Check if it contains alphabets
!/\d/.test(password) // Check if it contains numbers
) {
// If the password doesn't meet the requirements, prompt the user to enter a valid password
alert("Please enter a valid password that contains alphabets and numbers, does not start with a number, and is at least 6 characters long.");
} else {
// If the password meets the requirements, inform the user that it's valid
alert("Your password is valid.");
}
}
document.write("<br>");
document.write("<br>");
//question no 16
document.write("<h3>Question no 16</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques16()">Click Me</button>');
function ques16() {
var university = "University of Karachi";
var universityArray = university.split(" ");
alert("Elements of the array:<br>");
for (var i = 0; i < universityArray.length; i++) {
alert((i + 1) + ". " + universityArray[i] + "<br>");
}
}
document.write("<br>");
document.write("<br>");
//question no 17
document.write("<h3>Question no 17</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques17()">Click Me</button>');
function ques17() {
var userInput = prompt("Enter a string:");
if (userInput !== null && userInput !== "") {
var lastCharacter = userInput.charAt(userInput.length - 1);
alert("The last character of your input is: " + lastCharacter);
} else {
alert("No input provided. Please enter a string.");
}
}
document.write("<br>");
document.write("<br>");
//question no 18
document.write("<h3>Question no 18</h3>");
document.write("<br>");
document.write("<h4>Result :</h4>");
document.write("<br>");
document.write('<button id="btn" onclick="ques18()">Click Me</button>');
function ques18() {
var sentence = "The quick brown fox jumps over the lazy dog";
var lowercaseSentence = sentence.toLowerCase();
var wordToSearch = "the";
var words = lowercaseSentence.split(" ");
var count = 0;
for (var i = 0; i < words.length; i++) {
if (words[i] === wordToSearch) {
count++;
}
}
alert("The word '" + wordToSearch + "' occurs " + count + " times in the given string.");
console.log("The word '" + wordToSearch + "' occurs " + count + " times in the given string.");
}
document.write("<br>");
document.write("<br>");
document.write('<div style="color: blue;background-color: black;"><br><h1>Best OF luck<span style="font-size:50px;">👋</span></h1><br></div>')//👋