-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
637 lines (513 loc) · 16.5 KB
/
Copy pathapp.js
File metadata and controls
637 lines (513 loc) · 16.5 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/* // Challenge - 1
let firstName, lastName;
firstName = "Daniyal";
lastName = "Alam";
let address = "main street";
address = "first street";
console.log(
`My name is ${firstName} ${lastName} and my address is ${address}.`
);
// Challenge - 2 => Concatenation
let street = "10th Street";
let country = "Pakistan";
let fullMailingAddress = `My full address is ${street} ${country}.`;
console.log(fullMailingAddress);
// Challenge - 3
let score1 = 75;
let score2 = 85;
let score3 = 95;
let totalScore = score1 + score2 + score3;
let average = totalScore / 3;
console.log(`My total score is ${totalScore} and average is ${average}.`);
let plates = 20;
let people = 7;
let remainingPlates = 20 % 7;
remainingPlates++;
let message = "There are " + remainingPlates + " plates available.";
console.log(message); */
/* Chapter1.pdf */
/* 1. Write a script to greet your website visitor using JS alert box
let userName = prompt("Hi, what is your name?");
alert("Greetings, " + userName); */
/* 2.Invalid password
let userPassword = +prompt("Enter your password.");
if (userPassword === 211167) {
alert("Hurrah, your password has matched.");
} else {
alert("Error, please enter a correct password.");
} */
/* 3. Use line break
alert("Welcome to JS Land. \nHappy Coding!"); */
/* 4. Sequence of messages
alert("Welcome to JS Land.");
alert("Happy Coding!"); */
/* Chapter2.pdf */
/* let userName;
let myName = "Daniyal Alam";
let message;
message = "Hello World";
alert(message);
let studentName = "Hammad Alam";
let studentAge = "20 Years Old.";
let studentDomain = "Certified MERN Stack Developer";
alert(studentName);
alert(studentAge);
alert(studentDomain); */
/* let pizzaVar = "PIZZA \nPIZZ \nPIZ \nPI \nP";
alert(pizzaVar); */
/* // Email
let email = "daniyal.alam45@gmail.com";
alert("My email address is " + email); */
/* // Book
let book = "A smarter way to learn JavaScript";
alert(`I am trying to learn from the book ${book}`); */
// document.write("Yah, I can write HTML content through JS.");
/* // Show special string
var myString = "▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬▬";
document.write(myString);
alert(myString); */
/* Chapter: 6 to 9 */
/* let a = 10;
document.write("Result: \nThe value of a is: " + a);
document.write("<br>********************************");
a = ++a;
document.write("<br>The value of ++a is: " + a);
document.write("<br>Now the value of a is: " + a);
document.write("<br>");
document.write("<br>The value of a++ is: " + a);
a++;
document.write("<br>Now the value of a is: " + a);
document.write("<br>");
--a;
document.write("<br>The value of --a is: " + a);
document.write("<br>Now the value of a is: " + a);
document.write("<br>");
document.write("<br>The value of a-- is: " + a);
a--;
document.write("<br>Now the value of a is: " + a); */
//2.
/* var a = 2;
var b = 1;
var result = --a - --b + ++b + b--; //1 - 0 + 1 + 1 = 3
console.log(a, b, result); */
/* //3.
let userName = prompt("Enter your name: ");
alert("Greetings, " + userName); */
/* // 4. table of a number
var userInput = +prompt(
"Enter the number of which you want multiplication",
"5"
);
document.write(`${userInput} * 1 = ${userInput * 1}<br>`);
document.write(`${userInput} * 2 = ${userInput * 2}<br>`);
document.write(`${userInput} * 3 = ${userInput * 3}<br>`);
document.write(`${userInput} * 4 = ${userInput * 4}<br>`);
document.write(`${userInput} * 5 = ${userInput * 5}<br>`);
document.write(`${userInput} * 6 = ${userInput * 6}<br>`);
document.write(`${userInput} * 7 = ${userInput * 7}<br>`);
document.write(`${userInput} * 8 = ${userInput * 8}<br>`);
document.write(`${userInput} * 9 = ${userInput * 9}<br>`);
document.write(`${userInput} * 10 = ${userInput * 10}<br>`); */
/* //6.
var subjectOne, subjectTwo, subjectThree;
subjectOne = prompt("Enter subject one name: ");
subjectTwo = prompt("Enter subject two name: ");
subjectThree = prompt("Enter subject three name: ");
var totalMarksPerSubject = 100;
var obtainedMarksSubjectOne, obtainedMarksSubjectTwo, obtainedMarksSubjectThree;
obtainedMarksSubjectOne = +prompt("Enter obtained marks of " + subjectOne);
obtainedMarksSubjectTwo = +prompt("Enter obtained marks of " + subjectTwo);
obtainedMarksSubjectThree = +prompt("Enter obtained marks of " + subjectThree);
var totalObtainedMarks =
obtainedMarksSubjectOne + obtainedMarksSubjectTwo + obtainedMarksSubjectThree;
var percentage = (totalObtainedMarks / (totalMarksPerSubject * 3)) * 100;
document.write(`You got ${totalObtainedMarks} marks with ${percentage}%.`); */
// Chapter 3.pdf
/* // 1.
var age = 23;
alert("I am " + age + " year old."); */
/* // 2. number of visits
let visitCountNo = localStorage.getItem("visitCountNo");
if (visitCountNo === null) {
visitCountNo = 0;
}
visitCountNo++;
localStorage.setItem("visitCountNo", visitCountNo);
alert(`You have visited this site ${visitCountNo} times.`); */
/* // 3. birthyear
let birthYear = 2001;
document.write(`My birth year is ${birthYear}<br>`);
document.write(
`Data type of declared variable birthYear is ${typeof birthYear}`
); */
/* // 4. Online store
let visitorName = prompt("Hi, enter your name: ");
let productTitle = prompt("Enter product name you purchased: ");
let productQuantity = +prompt(
"How much quantity you want to purchase of " + productTitle
);
let message = `${visitorName} ordered ${productQuantity} ${productTitle} from White House Closet.`;
document.write(message); */
/* Chapter 9=11 */
/* // 1. welcome to city
let cityName = prompt("Enter your city, where you live?");
if (cityName === "Karachi") {
alert("Welcome to the city of lights, " + cityName);
} */
/* // 2. Gender
let gender = prompt("Enter your gender.");
if (gender === "male") {
alert("Good Morning Sir!");
} else {
alert("Good Morning Madam!");
}
*/
/* // 3. signal
let colorOfTrafficSignal = prompt(
"What color is now on traffic signal, let me know."
);
if (colorOfTrafficSignal === "red") {
alert("Must stop, don't move, this may cause risk.");
} else if (colorOfTrafficSignal === "yellow") {
alert("Hold on, but be ready to move.");
} else if (colorOfTrafficSignal === "green") {
alert("Hey, move now otherwise you have to wait 2 minutes more:)");
} else alert("Invalid input signal!"); */
/* // 4. fuel
let remainingFuel = +prompt("Tell me your car fuel in litres?");
if (remainingFuel < 0.25) {
alert("Hurry up, refill the fuel tank in your car.");
} */
// 5. outputs
// a.
/* var a = 4;
if (++a === 5) {
alert("Given condition for a is true"); //true
} */
/* // b.
var b = 82;
if (b++ === 83) {
alert("Given condition for b is true"); //false
} */
/* // c.
var c = 12;
if (c++ === 13) {
alert("Given condition 1 is true"); //false
}
if (c === 13) {
alert("Given condition 2 is true"); //true
}
if (++c < 14) {
alert("Given condition 3 is true"); //false
}
if (c === 14) {
alert("Given condition 4 is true"); //true
} */
/* // d.
var materialCost = 20000;
var laborCost = 2000;
var totalCost = materialCost + laborCost;
if (totalCost === materialCost + laborCost) {
alert("The cost is equal.");
} */
/* // e.
if (true) {
alert("True");
}
if (false) {
alert("False");
}
*/
/* // f.
if ("car" < "cat") {
alert("Car is less than Cat!"); //The code compares the strings `"car"` and `"cat"` lexicographically (based on the alphabetical order of characters), and since `"r"` comes before `"t"`, the condition is true, so it triggers the `alert` saying "Car is less than Cat!"
} */
/* // 6. Marksheet
let physics,
chemistry,
english,
totalMarks,
marksObtained,
percentage,
grade,
remarks;
totalMarks = +prompt("Enter total marks of subjects");
physics = Number(window.prompt("Please enter your marks in Physics"));
chemistry = Number(window.prompt("Please enter your marks in Chemistry"));
english = Number(window.prompt("Please enter your marks in English"));
marksObtained = physics + chemistry + english;
percentage = (marksObtained / totalMarks) * 100;
document.write("<br>Marks in Physics :" + physics);
document.write("<br>Marks in Chemistry :" + chemistry);
document.write("<br>Marks in English :" + english);
document.write("<br>Total Marks :" + totalMarks);
document.write("<br>Marks Obtained :" + marksObtained);
document.write("<br>Percentage :" + percentage);
switch (true) {
case percentage >= 80:
grade = "A-One";
remarks = "Excellent";
break;
case percentage >= 70:
grade = "A";
remarks = "Good";
break;
case percentage >= 60:
grade = "B";
remarks = "You need to improve";
break;
case percentage < 60:
grade = "Fail";
remarks = "Sorry";
break;
default:
grade = "Not know";
remarks = "No remarks";
break;
}
document.write("<br>Grade :" + grade);
document.write("<br>Remarks :" + remarks); */
/* // 7. Guess Game
const secretNumber = 8;
var userGuess = Number(window.prompt("Guess the number(1 to 10):"));
if (secretNumber === userGuess) {
alert("Bingo, correct answer.");
} else if (userGuess + 1 === secretNumber) {
alert("Close enough to the correct answer");
} else {
alert("Sorry, wrong guess. Try again!");
} */
/* // 8. Divisible by 3
var userInput = Number(prompt("Enter number:"));
if (userInput % 3 === 0) {
alert(`${userInput} is divisible by 3.`);
} else {
alert(`${userInput} is not divisible by 3.`);
} */
/* // 9. Even or Odd
var userInput = Number(prompt("Enter number:"));
if (userInput % 2 == 0) {
alert(`${userInput} is even number.`);
} else {
alert(`${userInput} is odd number.`);
} */
/* // 10. Temperature
var roomTemperature = Number(prompt("Enter room temperature:"));
if (roomTemperature > 40) {
alert("It's too hot outside.");
} else if (roomTemperature > 30) {
alert("The weather today is normal.");
} else if (roomTemperature > 20) {
alert("Today's weather is cool.");
} else if (roomTemperature > 10) {
alert("OMG! Today's weather is so cool.");
} else {
alert("undefined.");
} */
/* // 11. Calculator
var firstNumber, secondNumber, operationPerform, result;
firstNumber = Number(prompt("Enter first number:"));
secondNumber = Number(prompt("Enter second number:"));
operationPerform = prompt(
"Select the operation you want to perform: + | - | * | /"
);
if (operationPerform === "+") {
result = firstNumber + secondNumber;
document.write(`${firstNumber} + ${secondNumber} = ${result}`);
} else if (operationPerform === "-") {
result = firstNumber - secondNumber;
document.write(`${firstNumber} - ${secondNumber} = ${result}`);
} else if (operationPerform === "*") {
result = firstNumber * secondNumber;
document.write(`${firstNumber} * ${secondNumber} = ${result}`);
} else {
result = firstNumber / secondNumber;
document.write(`${firstNumber} / ${secondNumber} = ${result}`);
} */
// Chapter 12 and 13
/* // 1.
var userInput = window.prompt(
"Enter any number, uppercase character or lowercase character to identify:"
);
if (userInput.charCodeAt(0) >= "97" && userInput.charCodeAt(0) <= "122") {
alert("Entered character is lowercase.");
} else if (userInput.charCodeAt(0) >= "65" && userInput.charCodeAt(0) <= "90") {
alert("Entered character is uppercase.");
} else if (userInput >= 0) {
alert("Number is positive.");
} else {
alert("Number is negative.");
} */
// 2.
/* var firstInt, secondInt;
firstInt = +prompt("Enter first number: ");
secondInt = +prompt("Enter second number: ");
if (firstInt === secondInt) {
alert("Both are equal");
} else if (firstInt > secondInt) {
alert("First number is greater than second number.");
} else {
alert("Second number is greater.");
}
*/
/* // 4. vowel
var char = prompt("Enter character which should be of length 1: ");
if (char.toString().length == 1) {
if (
char.charCodeAt(0) == "65" ||
char.charCodeAt(0) == "69" ||
char.charCodeAt(0) == "73" ||
char.charCodeAt(0) == "79" ||
char.charCodeAt(0) == "85"
) {
alert("Char is of length 1 and also it is vowel.");
} else if (
char.charCodeAt(0) == "97" ||
char.charCodeAt(0) == "101" ||
char.charCodeAt(0) == "105" ||
char.charCodeAt(0) == "111" ||
char.charCodeAt(0) == "117"
) {
alert("Char is of length 1 and also it is vowel.");
} else {
alert("It is constant.");
}
} else {
alert("character should be of length 1.");
} */
/* // 5. Correct password
var correctPass = "nova123";
var userPass = prompt("Enter your password:");
if (userPass !== "") {
if (userPass === correctPass) {
alert("The password you entered matches the original password.");
} else {
alert("Incorrect password.");
}
} else {
alert("Kindly enter your password.");
} */
// chapter 14 - 16
/* // Array
let studentNames = [];
let studentDomains = new Array();
var stringArrays = ["Karachi", "London", "Makkah", "Madina"];
var numberArrays = [4, 5, 6, 7, 8];
var booleanArrays = [true, false, true, false];
var mixedArray = [1, "Daniyal", true];
var educationQualificationsInPakistan = [
"SSC",
"HSC",
"BS",
"BCS",
"BCOM",
"MS",
"PhD",
];
for (let index = 0; index < educationQualificationsInPakistan.length; index++) {
document.write("<li>" + educationQualificationsInPakistan[index] + "</li>");
} */
/* // 8.
var studentNames = ["Daniyal", "Hammad", "Noor Alam"];
var studentScores = [320, 280, 390];
var studentPercentages = [
(studentScores[0] / 500) * 100,
(studentScores[1] / 500) * 100,
(studentScores[2] / 500) * 100,
];
for (let index = 0; index < studentNames.length; index++) {
document.write(
`<li>Score of ${studentNames[index]} is ${studentScores[index]} and the percentage is ${studentPercentages[index]}</li>`
);
}
var foo = [];
foo[100] = 85;
document.write(foo.length); */
/* // 9.
var colorNames = ["Violet", "Vue Green", "Swelte Orange", "Pink"];
document.write(colorNames + "=> color names array");
// ask the user to add the color in the beginning
var colorAddStart = prompt("What color you want to add in the start?");
colorNames.unshift(colorAddStart);
document.write("<br>");
document.write("color adding in the start using unshift:");
document.write("<br>");
document.write(colorNames);
document.write("<br>");
// ask the user to add the color in the end
var colorAddEnd = prompt("What color you want to add at the end?");
colorNames.push(colorAddEnd);
document.write("<br>");
document.write("color adding at the end using push:");
document.write("<br>");
document.write(colorNames);
document.write("<br>");
// add two more colors at the beginning
colorNames.unshift(colorAddStart, colorAddEnd);
document.write("<br>");
document.write("two color adding in the start using unshift:");
document.write("<br>");
document.write(colorNames);
document.write("<br>");
// Use the shift method to remove an element from the beginning of an array.
colorNames.shift();
document.write("<br>");
document.write("Deleting the first element in the array:");
document.write("<br>");
document.write(colorNames);
document.write("<br>");
// delete the last color in the array
colorNames.pop();
document.write("<br>");
document.write("Deleting the last element in the array:");
document.write("<br>");
document.write(colorNames);
document.write("<br>");
// ask the user at which index he wants to add color and color name
var askUserIndex = +prompt("At which index you want to add color?");
var askUserColor = prompt("Which color you want to add?");
document.write("<br>");
colorNames.splice(askUserIndex, 0, askUserColor);
document.write("Array after inserting at specific index");
document.write("<br>");
document.write(colorNames);
document.write("<br>");
// ask the user at which index he wants to delete color and and how many
var askUserIndex = +prompt("At which index you want to delete color?");
var askUserDelete = +prompt(
"how many colors you want to delete from that index?"
);
document.write("<br>");
colorNames.splice(askUserIndex, askUserDelete);
document.write("Array after deleting from specific index");
document.write("<br>");
document.write(colorNames);
document.write("<br>"); */
/* // 10. sort
let studentScores = [320, 230, 130, 420, 10];
document.write(studentScores.sort() + " sorted array"); */
/* // 11. slice
var colorNames = ["Violet", "Vue Green", "Swelte Orange", "Pink"];
document.write(colorNames + "=> color names array");
document.write("<br>");
var newCopiedArray = colorNames.slice(1, 4);
document.write(
newCopiedArray + "=> New color names array copied from previous array"
); */
/* var arrayJoining = ["Hi ", "I am ", "Doing ", "Array Assignment"];
document.write(arrayJoining.join() + " array after join");
*/
/* // 13.
var educationQualificationsInPakistan = [
"SSC",
"HSC",
"BS",
"BCS",
"BCOM",
"MS",
"PhD",
];
for (let index = 0; index < educationQualificationsInPakistan.length; index++) {
document.write(educationQualificationsInPakistan.slice(index, index + 1));
document.write("<br>");
} */