-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
2127 lines (2056 loc) · 76.2 KB
/
Copy pathscript.js
File metadata and controls
2127 lines (2056 loc) · 76.2 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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// task 1
function task1() {
const today = new Date(); // take newDate
const dayName = new Array(7); // make array with day names
dayName[0] = "Sunday";
dayName[1] = "Monday";
dayName[2] = "Tuesday";
dayName[3] = "Wednesday";
dayName[4] = "Thursday";
dayName[5] = "Friday";
dayName[6] = "Saturday";
var n = dayName[today.getDay()]; // take specific day from array
const currentTime = new this.Date(); // take the rest of dates
const hh =
currentTime.getHours() < 10 ?
"0" + currentTime.getHours() :
currentTime.getHours();
const mm =
currentTime.getMinutes() < 10 ?
"0" + currentTime.getMinutes() :
currentTime.getMinutes();
const ss =
currentTime.getSeconds() < 10 ?
"0" + currentTime.getSeconds() :
currentTime.getSeconds();
// AM /PM cant be const. let is allowed.
let amPm = " ";
if (hh >= 12) {
amPm += "PM";
} else {
amPm += "AM";
}
// show outcome inside html element
document.getElementById("task1").innerHTML = `Today is:<br/> ${n}<br/> Current time is: <br/> ${hh}${amPm} : ${mm} : ${ss}`;
}
setInterval(task1, 1000);
task1();
//task 3
// take newDate
var d = new Date();
var yy = d.getFullYear();
var mo = ("0" + (d.getMonth() + 1)).slice(-2); //d.getMonth() + 1;
var dd = ("0" + d.getDate()).slice(-2); // d.getDate();
const ans3 = document.getElementById("task3");
ans3.innerHTML = dd + "-" + mo + "-" + yy;
//task 4
function task4() {
var aa = 5;
var bb = 6;
var cc = 7;
// hernon formula for triangle area
var ar = (aa + bb + cc) / 2;
var area = Math.sqrt(ar * ((ar - aa) * (ar - bb) * (ar - cc)));
document.getElementById("task4").innerHTML = area;
}
task4();
setInterval(function () {
const str = "w3resource";
document.getElementById("task5").textContent = str.slice(1) + str.slice(0, 1)
}, 1000)
//task6
function checkYear() {
//take value
var leap = document.getElementById("leapYear").value;
// document.getElementById("againLeap").innerHTML = leap;
var leapFour = 4;
// check if value is number
if (isNaN(leap)) {
document.getElementById("againLeap").innerHTML = "Type numbers only";
document.getElementById("task6").style.backgroundColor = "#d9534f";
// check if its empty string
} else if (leap == "") {
document.getElementById("againLeap").innerHTML = "Type any number";
document.getElementById("task6").style.backgroundColor = "#5bc0de";
}
// leap year is divisionable by4
else if (leap % leapFour == 0) {
document.getElementById("againLeap").innerHTML = "This is Leap Year!";
document.getElementById("task6").style.backgroundColor = "#5cb85c";
} else {
//if (leap % leapFour != 0)
document.getElementById("againLeap").innerHTML = "This is not Leap Year!";
document.getElementById("task6").style.backgroundColor = "#f0ad4e";
}
}
function task7() {
// loop from 2014 to 2050, count every year
for (var year = 2014; year <= 2050; year++) {
// find specific month and day
var firstJan = new Date(year, 0, 1);
if (firstJan.getDay() === 0)
document.getElementById("task7").innerHTML += `${year} \n`;
// "1st January is being a Sunday " + year;
}
}
task7();
//task8
function guessNumber() {
// take value
var givenNumber = document.getElementById("takeNumber").value;
// random from 1-10
var rand = Math.floor(Math.random() * 10);
document.getElementById("guessOutcome").innerHTML = rand;
if (isNaN(givenNumber)) {
document.getElementById("guessOutcome").innerHTML = "Type number!";
document.getElementById("task8").style.backgroundColor = "#f0ad4e";
document.getElementById("takeNumber").value = "";
} else if (givenNumber == rand) {
document.getElementById("guessOutcome").innerHTML =
"Good work! You hit " + rand;
document.getElementById("task8").style.backgroundColor = "#5cb85c";
document.getElementById("takeNumber").value = "";
} else {
document.getElementById("guessOutcome").innerHTML =
"Not matched! The number was: " + rand;
document.getElementById("task8").style.backgroundColor = "#d9534f";
document.getElementById("takeNumber").value = "";
}
}
//task9
function task9() {
var dayNow = new Date();
var toDay = dayNow.getTime();
// var christmas_day = new Date(present_date.getFullYear(), 11, 25)
var xmas = new Date("2021,12,25");
var xmasDay = xmas.getTime();
//one day in miliseconds
var oneDay = 1000 * 60 * 60 * 24;
var howManyDays = Math.ceil((xmasDay - toDay) / oneDay);
document.getElementById("task9").innerHTML = howManyDays;
}
task9();
//task 10
function multiply() {
firstNumber = document.getElementById("firstNumber").value;
secondNumber = document.getElementById("secondNumber").value;
document.getElementById("task10outcome").innerHTML =
firstNumber * secondNumber;
}
function divide() {
firstNumber = document.getElementById("firstNumber").value;
secondNumber = document.getElementById("secondNumber").value;
document.getElementById("task10outcome").innerHTML =
firstNumber / secondNumber;
}
//task11
function celsius() {
cel = document.getElementById("celsius").value;
if (cel == "") {
document.getElementById("celfah").innerHTML = "Emplty value! Try again";
} else {
celConv = (cel - 32) / 1.8;
document.getElementById("celfah").innerHTML =
cel + " Celsius is:" + "</br>" + celConv + "</br>" + "Fahrenheit";
document.getElementById("fahrenheit").value = "";
}
}
function fahrenheit() {
fah = document.getElementById("fahrenheit").value;
if (fah == "") {
document.getElementById("celfah").innerHTML = "Emplty value! Try again";
} else {
fahConv = (fah * 9) / 5 + 32;
document.getElementById("celfah").innerHTML =
fah + " Fahrenheit is:" + "</br>" + fahConv + "</br>" + "Celsius";
document.getElementById("celsius").value = "";
}
}
//task12
//function getUrl (){
// document.getElementById("outcomeUrl").innerHTML= window.location.href;
//};
//task13
var var_name = "userName";
var n = 120;
this[var_name] = n;
//console.log(this[var_name])
//task14
function getExtension(filename) {
filename = "index.html";
document.getElementById("extension").innerHTML = filename.split(".").pop();
}
//task15
function divideThirteen() {
someNumber = document.getElementById("thirteen").value;
difference = someNumber - 13;
if (someNumber > 13) {
document.getElementById("showthirteen").innerHTML = difference * 2;
} else if (someNumber < 13) {
document.getElementById("showthirteen").innerHTML = difference;
} else {
document.getElementById("showthirteen").innerHTML = "Type number only";
}
}
//task16
function sumNumber() {
sumNumberone = document.getElementById("sumNumberone").value;
sumNumbertwo = document.getElementById("sumNumbertwo").value;
sumNumbers = parseInt(sumNumberone) + parseInt(sumNumbertwo);
if (isNaN(sumNumberone)) {
document.getElementById("sumAnswer").innerHTML = "Numbers only";
} else if (isNaN(sumNumbertwo)) {
document.getElementById("sumAnswer").innerHTML = "Numbers only";
} else if (sumNumbers > sumNumberone) {
document.getElementById("sumAnswer").innerHTML = sumNumbers * 3;
} else {
document.getElementById("sumAnswer").innerHTML = sumNumbers;
}
}
//taks17
function computeNum() {
computeOne = document.getElementById("computeOne").value;
computeTwo = document.getElementById("computeTwo").value;
diffNumber = parseInt(computeOne) - parseInt(computeTwo);
if (isNaN(computeOne)) {
document.getElementById("compute").innerHTML = "Numbers only";
} else if (isNaN(computeTwo)) {
document.getElementById("compute").innerHTML = "Numbers only";
} else if (diffNumber > 19) {
document.getElementById("compute").innerHTML = diffNumber * 3;
} else {
document.getElementById("compute").innerHTML = diffNumber;
}
}
//task18
function task18true(numtask18a, numtask18b) {
numtask18a = document.getElementById("task18a").value;
numtask18b = document.getElementById("task18b").value;
sumtask18 = parseInt(numtask18a) + parseInt(numtask18b);
if (numtask18a + numtask18b === 50 || numtask18a == 50 || numtask18b == 50) {
document.getElementById("task18").innerHTML = "true";
} else {
document.getElementById("task18").innerHTML = "false";
}
}
//task19
function task19() {
numtask19 = document.getElementById("task19a").value;
if (Math.abs(100 - numtask19) <= 20 || Math.abs(400 - numtask19) <= 20) {
document.getElementById("task19ans").innerHTML = "true";
} else {
document.getElementById("task19ans").innerHTML = "false";
}
}
//task20
function task20() {
numtask20a = document.getElementById("task20a").value;
numtask20b = document.getElementById("task20b").value;
if (numtask20a > 0 && numtask20b > 0) {
document.getElementById("task20ans").innerHTML = "false";
} else if (numtask20a < 0 && numtask20b < 0) {
document.getElementById("task20ans").innerHTML = "false";
} else {
document.getElementById("task20ans").innerHTML = "true";
}
}
//task21
function task21() {
var task21value = document.getElementById("task21").value;
var findPY = task21value.charAt(0) + task21value.charAt(1);
var lettersPY = "Py";
if (findPY == "py" || findPY == "Py") {
document.getElementById("task21ans").innerHTML = task21value;
} else {
document.getElementById("task21ans").innerHTML = lettersPY.concat(
task21value
);
}
}
//task22
function task22() {
task22value = document.getElementById("task22").value;
task22random = Math.floor(Math.random() * task22value.length) + 1;
//document.getElementById("task22ans").innerHTML = task22random;
document.getElementById("task22ans").innerHTML =
"Removed random character number: " + task22random;
// slice method for showing the first part od string
task22firstpart = task22value.slice(0, task22random - 1);
// substring will take second part of string and hides the first part -
document.getElementById("task22ansB").innerHTML =
"Output: " + task22firstpart + task22value.substring(task22random);
}
//task23
function task23() {
task23word = document.getElementById("task23").value;
//check length of string
if (task23word.length <= 1) {
document.getElementById("task23ans").innerHTML = "Word has to be longer!";
} else {
document.getElementById("task23ans").innerHTML =
task23word.slice(-1) +
task23word.substring(1, task23word.length - 1) +
task23word.charAt(0);
}
}
//task24
function task24() {
task24word = document.getElementById("task24").value;
task24letter = task24word.charAt(0);
document.getElementById("task24ans").innerHTML =
task24letter + task24word + task24letter;
}
//task25
function task25() {
task25word = document.getElementById("task25").value;
task25letter = task25word.substring(0, 3);
if (task25word.length < 3) {
document.getElementById("task25ans").innerHTML = "Word is too short!";
} else {
document.getElementById("task25ans").innerHTML =
task25letter + task25word + task25letter;
}
}
//task26
function task26() {
task26number = document.getElementById("task26").value;
if (isNaN(task26number)) {
document.getElementById("task26ans").innerHTML = "Only numbers!";
} else if (task26number % 3 === 0 || task26number % 7 === 0) {
document.getElementById("task26ans").innerHTML = "true";
} else {
document.getElementById("task26ans").innerHTML = "false";
}
}
//task27
function task27() {
task27word = document.getElementById("task27").value;
check27word = task27word.substring(0, 4); //take first 4 letters
if (check27word === "Java" || check27word === "java") {
document.getElementById("task27ans").innerHTML = "true";
} else {
document.getElementById("task27ans").innerHTML = "false";
}
}
//task28
function task28() {
var el = document.getElementById("task28a");
var val = el.value.split(" "); //seperate string to array, divided by space
el.value = val.reduce((a, c) => a + Number(c), 0); // reduce will add two or more numbers
if (el.value >= 50 && el.value <= 99) {
document.getElementById("task28ans").innerHTML = "true";
} else {
document.getElementById("task28ans").innerHTML = "false";
}
}
//task29
function task29() {
var el = document.getElementById("task29");
var val = el.value.split(" ");
el.value = val.reduce((a, c) => a + Number(c), 0);
if (el.value >= 50 && el.value <= 99) {
document.getElementById("task29ans").innerHTML = "true";
}
}
function task30() {
const task30word = document.getElementById("task30").value;
word30 = task30word.slice(4, 10); //take "letters"
if (word30 == "script") {
document.getElementById("task30ans").innerHTML = task30word.substr(0, 4);
} else {
document.getElementById("task30ans").innerHTML = task30word;
}
}
//task31
function task31() {
var el = document.getElementById("task31");
var val = el.value.split(" ").sort(); //arrays first, and sort for put numbers in order
document.getElementById("task31ans").innerHTML = val[val.length - 1]; //show last number - the highest
}
//task32
function task32() {
var el = document.getElementById("task32");
var val = el.value.split(" ").sort();
if (val[0] <= 99 && val[1] <= 99) {
document.getElementById("task32ans").innerHTML = val[val.length - 1];
} else if (val[0] >= 100 || val[1] >= 100) {
document.getElementById("task32ans").innerHTML = "numbers are 100 or over!";
}
}
//task33
function task33() {
var el = document.getElementById("task33");
var val = el.value.split(" ").sort();
if (
(val[0] >= 40 || val[0] <= 60 || val[1] >= 40 || val[1] <= 60) &&
(val[0] >= 70 || val[0] <= 100 || val[1] >= 70 || val[1] <= 100)
) {
document.getElementById(
"task33ans"
).innerHTML = `true, these numbers are: ${val[0]} and ${val[1]}`;
} else {
document.getElementById("task33ans").innerHTML = "false";
}
}
//task34
function task34() {
var el = document.getElementById("task34");
var val = el.value.split(" ").sort();
if (
val[0] >= 40 &&
val[0] <= 60 &&
val[1] >= 40 &&
val[1] <= 60 //&&
//val[0] <= 60 && val[1] >= 40 || val[1] <= 60
) {
document.getElementById(
"task34ans"
).innerHTML = `larger number is ${val[1]}`;
} else {
document.getElementById("task34ans").innerHTML = "not in range 40 - 60";
}
}
//task35
function task35() {
task35word = document.getElementById("task35a").value;
task35char = document.getElementById("task35b").value;
takecharOne = task35word.charAt(1);
takecharTwo = task35word.charAt(3);
if (task35char === takecharTwo && task35char === takecharTwo) {
document.getElementById("task35ans").innerHTML = "true";
} else {
document.getElementById("task35ans").innerHTML = "false";
}
}
//task36
function task36() {
const el = document.getElementById("task36");
const val = el.value.split(" "); //spilt by space
const first = val[0].slice(-1); //take last digit from each nummbers
const second = val[1].slice(-1);
const third = val[2].slice(-1);
if (first === second && first === third) {
document.getElementById("task36ans").innerHTML =
"True: all numbers has " + first;
} else {
document.getElementById(
"task36ans"
).innerHTML = `False, all numbers are different: ${first}, ${second}, ${third}`;
}
}
//task37
task37 = () => {
const task37word = document.getElementById("task37").value;
if (task37word.length <= 3) {
document.getElementById("task37ans").innerHTML = task37word.toUpperCase();
} else {
document.getElementById("task37ans").innerHTML =
task37word.slice(0, 3).toLowerCase() +
task37word.substr(3, task37word.length).toUpperCase();
}
};
//task38
task38 = () => {
let rand = Math.floor(Math.random() * 100);
if (rand > 89 && rand <= 100) {
document.getElementById(
"task38ans"
).innerHTML = `Success! Student gets ${rand} points! Grade: A+`;
} else {
document.getElementById(
"task38ans"
).innerHTML = `Student gets ${rand} points, failure`;
}
};
//taks39
task39 = () => {
let el = document.getElementById("task39");
let val = el.value.split(" ");
el.value = val.reduce((a, c) => a + Number(c), 0);
if (el.value >= 50 && el.value <= 99) {
document.getElementById("task39ans").innerHTML = "65";
} else {
document.getElementById("task39ans").innerHTML = "80";
}
};
//task40
task40 = () => {
let el = document.getElementById("task40");
let val = el.value.split(" ");
el.value = val.reduce((a, c) => a + Number(c), 0);
if (val[0] == 8 || val[1] == 8 || el.value == 8) {
document.getElementById("task40ans").innerHTML = "true";
} else {
document.getElementById("task40ans").innerHTML = "false";
}
};
//task41
task41 = () => {
let el = document.getElementById("task41");
let val = el.value.split(" ");
el.value = val.reduce((a, c) => a + Number(c), 0);
if ((val[0] == val[1]) == val[2]) {
document.getElementById("task41ans").innerHTML = "30";
} else if (val[0] == val[1] || val[0] == val[2] || val[1] == val[2]) {
document.getElementById("task41ans").innerHTML = "40";
} else {
document.getElementById("task41ans").innerHTML = "20";
}
};
task42 = () => {
let el = document.getElementById("task42");
let val = el.value.split(" ");
//el.value = val.reduce((a, c) => a + Number(c), 0);
if (val[0] < val[1] && val[1] < val[2]) {
document.getElementById("task42ans").innerHTML = "Strict mode";
} else {
document.getElementById("task42ans").innerHTML = "Soft mode";
}
};
task43 = () => {
const el = document.getElementById("task43");
const val = el.value.split(" ");
const task43ans = document.getElementById("task43ans");
const first = val[0];
const second = val[1];
const third = val[2];
if (first.length === 1 || second.length === 1 || third.length === 1) {
if (first === second || second === third) {
task43ans.textContent = `Two or more digits are same`;
} else {
task43ans.textContent = "Rightmost digits are not the same";
}
} else if (
first.slice(-1) === second.slice(-1) ||
first.slice(-1) === third.slice(-1)
) {
task43ans.textContent = `Two or more digits are same`;
} else {
task43ans.textContent = "Rightmost digits are not the same";
}
};
task44 = () => {
let el = document.getElementById("task44");
let val = el.value.split(" ");
if (val[0] >= 20 && val[0] < val[1] && val[0] < val[2]) {
document.getElementById("task44ans").innerHTML = "true";
} else {
document.getElementById("task44ans").innerHTML = "false";
}
};
task45 = () => {
let el = document.getElementById("task45");
let val = el.value.split(" ");
el.value = val.reduce((a, c) => a + Number(c), 0);
if (val[0] == 15 || val[1] == 15 || el.value == 15 || val[0] - val[1] == 15) {
document.getElementById("task45ans").innerHTML = "true";
} else {
document.getElementById("task45ans").innerHTML = "false";
}
};
task46 = () => {
let el = document.getElementById("task46");
let val = el.value.split(" ");
if (
(val[0] % 7 == 0 || val[0] % 11 == 0) &&
(val[1] % 7 == 0 || val[1] % 11 == 0)
) {
document.getElementById("task46ans").innerHTML = "true";
} else {
document.getElementById("task46ans").innerHTML = "false";
}
};
task47 = () => {
let el = document.getElementById("task47");
let val = el.value.split(" ");
if (val[0] >= 40 && val[0] <= 10000) {
document.getElementById("task47ans").innerHTML = "true";
} else {
document.getElementById("task47ans").innerHTML = "false";
}
};
task48 = () => {
task48word = document.getElementById("task48").value;
document.getElementById("task48ans").innerHTML = task48word
.split("")
.reverse()
.join("");
//devide string to single characters, than reverse characters. join method will delete commas from array.
};
//replace every character in a given string with the character following it in the alphabet
task49 = () => {
let letters = document.getElementById("task49").value;
// let arr will change letter to small
// divided to array and joined to string
//let arr = letters.toLowerCase().split(" ").join(",");
//
let arr = letters.toLowerCase();
for (let i = 0; i < arr.length; i++) {
// fromcharcode is to find letter by Unicode
//charcodeat is to find unicode by letter
document.getElementById("task49ans").textContent += String.fromCharCode(
1 + letters.charCodeAt(i)
);
}
};
// capitalize the first letter of each word
task50 = () => {
let task50word = document.getElementById("task50").value;
let arr = task50word.toLowerCase().split(" "); //first to small letters
for (let i = 0; i <= arr.length; i++) {
arr[i] = arr[i][0].toUpperCase() + arr[i].substr(1);
// divided string to array and every element inside array arr[i], will give big first letter [0] and will be concatenate with the rest of the arr.
document.getElementById("task50ans").textContent = arr.join(" ");
}
};
//convert a given number to hours and minutes
task51 = () => {
let task51number = document.getElementById("task51").value;
let godziny = task51number / 60;
if (isNaN(task51number)) {
document.getElementById("task51ans").textContent = "numbers only";
} else {
document.getElementById("task51ans").textContent =
task51number +
" is " +
parseInt(godziny) +
" hours and " +
(task51number - parseInt(godziny) * 60) +
" minutes";
//parseInt(godziny) + (task51number - parseInt(godziny) * 60);
}
};
//convert the letters of a given string in alphabetical order
task52 = () => {
let task52word = document.getElementById("task52").value;
document.getElementById("task52ans").textContent = task52word
.split("")
.sort()
.join("");
};
// check characters a and b are separated by 3 places exactly
task53 = () => {
let task53word = document.getElementById("task53").value;
let task53a = task53word.indexOf("a");
let task53b = task53word.indexOf("b");
//|| task53b - task53a == 0 || (task53a + 1) - task53b == 3
if (task53b - (task53a + 1) == 3 || task53a - 1 - task53b == 3) {
document.getElementById("task53ans").textContent = "true";
} else {
document.getElementById("task53ans").textContent = task53a + " " + task53b;
}
};
//count the number of vowels in a given string.
//aeiou
task54 = () => {
let task54word = document.getElementById("task54").value;
let vowel = task54word.toLowerCase().match(/[aeiou]/g);
document.getElementById(
"task54ans"
).textContent = `String has ${vowel} which is ${vowel.length}`;
};
//given string contains equal number of p's and t's
// task55 = () => {
// const task55word = document.getElementById("task55").value;
// const task55ans = document.getElementById("task55ans")
// const wordP = task55word.toLowerCase().match(/p/g);
// const wordT = task55word.toLowerCase().match(/t/g);
// // let p = wordP.length;
// // let t = wordT.length;
// if (task55word == "") {
// task55ans.textContent = `adasd`
// } else if (wordP.length = !wordT.length) {
// task55ans.textContent = `not equal`
// } else {
// task55ans.textContent = `equal`
// }
// };
task55 = () => {
const task55word = document.getElementById("task55").value;
const task55ans = document.getElementById("task55ans");
const wordP = task55word.toLowerCase().match(/p/g) || "";
const wordT = task55word.toLowerCase().match(/t/g) || "";
const p = wordP.length;
const t = wordT.length;
if (task55word == "") {
task55ans.textContent = "empty";
} else if (p == 0 && t == 0) {
task55ans.textContent = "not equal";
} else if (p == t) {
task55ans.textContent = "equal";
}
// || p === 0 && t === 0
};
//divide two positive numbers and return a string with properly formatted commas
task56 = () => {
let task56word = document.getElementById("task56");
let num56 = task56word.value.split(" ");
let commas = num56[0] / num56[1];
document.getElementById("task56ans").textContent = commas
.toString()
.split("");
};
//create a new string of specified copies (positive number) of a given string.
task57 = () => {
let task57word = document.getElementById("task57").value;
let num57 = task57word.split(" ");
document.getElementById("task57ans").textContent = num57[0].repeat(num57[1]);
};
// create a new string of 4 copies of the last 3 characters of a given original string.
task58 = () => {
let task58word = document.getElementById("task58").value;
let piece = task58word.slice(-3);
if (task58word.length < 3) {
document.getElementById("task58ans").textContent = "Word is too short.";
} else {
document.getElementById("task58ans").textContent = piece.repeat(4);
}
};
//extract the first half of a string of even length
task59 = () => {
let task59word = document.getElementById("task59").value;
let half = task59word.length / 2;
if (task59word.length % 2 === 0) {
document.getElementById("task59ans").textContent = task59word.slice(
0,
half
);
} else {
document.getElementById("task59ans").textContent = `Divide is not possible`;
}
};
//reate a new string without the first and last character of a given string
task60 = () => {
let task60word = document.getElementById("task60").value;
document.getElementById("task60ans").textContent = task60word.slice(
1,
task60word.length - 1
);
};
//concatenate two strings except their first character
task61 = () => {
let task61word = document.getElementById("task61").value;
let word = task61word.split(" ");
let conc = word[0].toString().slice(1).concat(word[1].slice(1));
document.getElementById("task61ans").textContent = conc;
};
//move last three character to the start of a given string. The string length must be greater or equal to three.
task62 = () => {
let task62word = document.getElementById("task62").value;
let word = task62word.slice(-3);
let conc = task62word.substring(0, task62word.length - 3);
if (task62word.length < 3) {
document.getElementById(
"task62ans"
).textContent = `word should have 3 or more letters`;
} else {
document.getElementById("task62ans").textContent = word + conc;
}
};
//create a string using the middle three characters of a given string of odd length
task63 = () => {
let task63word = document.getElementById("task63").value;
//let word = task63word.slice(-3)
//let conc = task63word.substring(0, (task63word.length - 3))
if (task63word.length < 3) {
document.getElementById(
"task63ans"
).textContent = `word should have 3 or more letters`;
} else if (task63word.length % 2 === 0) {
document.getElementById("task63ans").textContent = task63word;
} else {
document.getElementById("task63ans").textContent = task63word.substr(
task63word.length / 2 - 1,
3
);
}
};
//concatenate two strings and return the result. If the length of the strings are not same then remove the characters from the longer string
task64 = () => {
let task64word = document.getElementById("task64");
let ww = task64word.value.split(" ");
let w1 = ww[1].length;
let w0 = ww[0].length;
if (ww[0].length > ww[1].length) {
document.getElementById("task64ans").textContent =
ww[0].substring(0, w1) + ww[1]; // ww[0].slice(w1)
} else if (ww[0].length < ww[1].length) {
document.getElementById("task64ans").textContent =
ww[0] + ww[1].substring(0, w0); //ww[1].slice(w0) +
} else {
document.getElementById("task64ans").textContent = ww[0] + ww[1];
}
};
//test whether a string end with "Script"
task65 = () => {
let task65word = document.getElementById("task65").value;
let pword = task65word.toLowerCase().slice(-6);
if (task65word.length < 6) {
document.getElementById("task65ans").textContent =
"word is too short! 6 letters is minimum";
} else if (pword === "script") {
document.getElementById("task65ans").textContent = "true";
} else {
document.getElementById("task65ans").textContent = "false";
}
};
//display the city name if the string begins with "Los" or "New"
task66 = () => {
let task66word = document.getElementById("task66").value;
let piece = task66word.toLowerCase().slice(0, 3);
if (piece == "new" || piece == "los") {
document.getElementById("task66ans").textContent = "true";
} else {
document.getElementById("task66ans").textContent = "";
}
};
//create a new string from a given string, removing the first and last characters of the string if the first or last character are 'P'
task67 = () => {
let task67word = document.getElementById("task67").value;
let piece = task67word.toLowerCase().charAt(0);
if (piece === "p") {
document.getElementById("task67ans").textContent = task67word.slice(
1,
task67word.length - 1
);
} else {
document.getElementById("task67ans").textContent = task67word;
}
};
//create a new string using the first and last n characters from a given sting.The string length must be greater or equal to n.
task68 = () => {
let task68w = document.getElementById("task68w").value;
let n68 = document.getElementById("task68n").value;
if (task68w.length >= n68) {
document.getElementById("task68ans").textContent = task68w.slice(
n68,
task68w.length - n68
);
} else {
document.getElementById("task68ans").textContent =
"The string length must be greater or equal to n.";
}
};
//compute the sum of three elements of a given array of integers of length 3.
task69 = () => {
let task69numbers = document.getElementById("task69").value;
let num = task69numbers.split(" ");
document.getElementById("task69ans").textContent = num.reduce(
(a, c) => a + Number(c),
0
);
};
//rotate the elements left of a given array of integers of length 3.
task70 = () => {
let task70number = document.getElementById("task70").value;
if (task70number.length === 3) {
let nowy = task70number.split("");
document.getElementById(
"task70ans"
).textContent = ` ${nowy[1]} ${nowy[2]} ${nowy[0]} `;
} else {
document.getElementById("task70ans").textContent = `3 numbers only!`;
}
};
//check whether 1 appears in first or last position of a given array of integers. The array length must be greater or equal to 1.
task71 = () => {
let task71number = document.getElementById("task71").value;
let nowy = task71number.split("");
if (nowy[0] == 1) {
document.getElementById("task71ans").textContent = `true`;
} else {
document.getElementById("task71ans").textContent = `false`;
}
};
//check whether the first and last elements are equal of a given array of integers length 3.
task72 = () => {
let task71number = document.getElementById("task72").value;
let nowy = task71number.split(" ");
if (nowy[0] === nowy[2]) {
document.getElementById("task72ans").textContent = `true`;
} else {
document.getElementById("task72ans").textContent = `false`;
}
};
//reverse the elements of a given array of integers length 3.
task73 = () => {
let task73number = document.getElementById("task73").value;
let nowy = task73number.split(" ");
if (nowy.length === 3) {
document.getElementById("task73ans").textContent = nowy.reverse();
} else {
document.getElementById("task73ans").textContent = `3 integers only!`;
}
};
//find the larger value between the first or last and set all the other elements with that value. Display the new array.
task74 = () => {
let task74number = document.getElementById("task74").value;
let nowy = task74number.split(" ");
nowy.sort(function (a, b) {
return a - b;
});
document.getElementById(
"task74ans"
).textContent = `${nowy[2]} ${nowy[2]} ${nowy[2]}`;
};
//create a new array taking the middle elements of the two arrays of integer and each length 3.
task75 = () => {
let task75number = document.getElementById("task75a").value;
let task75numberb = document.getElementById("task75b").value;
let nowy = task75number.split(" ");
let nowyb = task75numberb.split(" ");
document.getElementById("task75ans").textContent = `${nowy[1]} ${nowyb[1]} `;
};
//create a new array taking the first and last elements from a given array of integers and length must be greater or equal to 1.
task76 = () => {
let task76number = document.getElementById("task76").value;
let nowy = task76number.split(" ");
document.getElementById("task76ans").textContent = `${nowy[0]} ${nowy[nowy.length - 1]
}`;
};
//test whether an array of integers of length 2 contains 1 or a 3.
task77 = () => {
let task77number = document.getElementById("task77").value;
let nowy = task77number.split(" ");
if (nowy[0] == 1 || nowy[0] == 3 || nowy[1] == 1 || nowy[1] == 3) {
document.getElementById("task77ans").textContent = `true`;
} else {
document.getElementById("task77ans").textContent = `false`;
}
};
//test whether an array of integers of length 2 does not contain 1 or a 3.
task78 = () => {
let task78number = document.getElementById("task78").value;
let nowy = task78number.split(" ");
if (nowy[0] != 1 || nowy[0] != 3 || nowy[1] != 1 || nowy[1] != 3) {
document.getElementById("task78ans").textContent = `true`;
} else {
document.getElementById("task78ans").textContent = `false`;
}
};
//test whether a given array of integers contains 30 and 40 twice. The array length should be 0,1, or 2
task79 = () => {
let task79number = document.getElementById("task79").value;
let nowy = task79number.split(" ");
if ((nowy[0] == 30 && nowy[1] == 30) || (nowy[0] == 40 && nowy[1] == 40)) {
document.getElementById("task79ans").textContent = `true`;
} else {
document.getElementById("task79ans").textContent = `false`;
}
};
//swap the first and the last elements of given array of integers. The array length should be at least 1.
task80 = () => {
let task80number = document.getElementById("task80").value;
let nowy = task80number.split(" ");
document.getElementById("task80ans").textContent = `${nowy[nowy.length - 1]
},${nowy.slice(1, -1)},${nowy[0]}`;
};
//add two digits of a given positive integer of length two.
task81 = () => {
let task81number = document.getElementById("task81").value;
//let nowy = task81number.parseInt();
if (task81number.length != 2) {
document.getElementById(
"task81ans"
).textContent = `Integer shoud have 2 digits only`;
} else {
document.getElementById("task81ans").textContent =
parseInt(task81number.charAt(0)) + parseInt(task81number.charAt(1));
}
};
//Write a JavaScript to add two positive integers without carry.
task82 = () => {
let task82number = document.getElementById("task82").value;
let nowy = task82number.split(" ");
var result = 0,
xx = 1;
while (nowy[0] > 0 && nowy[1] > 0) {
result += xx * ((nowy[0] + nowy[1]) % 10);
n1 = Math.floor(nowy[0] / 10);
n2 = Math.floor(nowy[1] / 10);
xx *= 10;
}
document.getElementById("task82ans").textContent = result;
};
//Write a JavaScript to find the longest string from a given array of strings.
task83 = () => {
let task83number = document.getElementById("task83").value;
let nowy = task83number.split(" ").sort();
nowy.sort(function (a, b) {
return a - b;
});
document.getElementById("task83ans").textContent = nowy[nowy.length - 1];
};