-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
1362 lines (1003 loc) · 44.3 KB
/
test.js
File metadata and controls
1362 lines (1003 loc) · 44.3 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
console.log("Bismilla");
let count = 0;
console.log(count);
let myAge = 21
console.log(myAge)
//1.it's my personal things
let currentYear = 2021;
console.log(currentYear);
let myFatherAge = 52;
console.log(myFatherAge);
let count_false = 2+2;
console.log(count_false);
function sleep(name, time){
console.log(name + " is sleeping from " + time);
}
sleep("Shawn","5.30AM.");
sleep("Husky","11PM.");
function opps(name, age){
console.log(name + " is " + age + " years.");
}
opps("Shawn" ,"21");
opps("Zahin","21");
opps("Ashik", "24");
//start creating array
let cars = ["TOYOTA","BMW","AUDI","MARCEDES BENZ", "FORD"];
cars [0] ="Ferrari"; //changing element inside of array
cars.push("Lamborgini")
//IF I want to see the result I can do it here
console.log(cars);
//again creating function
function awake(name, time, reason){
console.log(name + " always awake before " + time + " that's means " + reason);
}
awake("Shawn","1PM", "he always try to pray in time");
awake("Zahin", "3PM","he is unaware about prayer");
awake("Emon", "5.30PM in the afternoon","he is out of sense,and little bit mental")
function myFunction(a, b){
return a * b;
}
let x = myFunction(4, 3);
//here, let x = 12; because it's the rules of function return
//we can also stored function data by doing this
{
const x = 10;
console.log(typeof x);
}
// Conversion Formula
// °Celsius = (5/9) * (°Fahrenheit - 32)
function hello(){
let beautifulCar = "Audi";
console.log(beautifulCar);
}
hello();
function wonderful(name, place, time, age, height, weight, comment){
console.log (name + " studying Computer Science & Technology " + place +
" He always awake at " + time + " He is " + age + " His height is " + height + " His weight is " + weight +
" His BMI is " + comment);
}
wonderful("Shawn","at Southwest Forerstry University.","1 PM.","21 years old.","172cm,","60kg.","good");
wonderful("Zahin","at Southwest Forerstry University.","5.30 PM.","21 years old.","172.5cm,","60kg.","good");
wonderful("Babul","at Buet.","6 AM.","23 years old.","170cm,","65kg.","good");
wonderful("Shain","at Green University.","8 AM.","22 years old.","172cm,","67kg.","good");
//Here,I'm going to practice one example from w3schools.com,so let's start our journy,
function toCelsius(f){
return (5/9) * (f-32);
}
toCelsius(77);
//let's create a js object variable
const car = {
name: "Fiat",
model: 500,
weight: "2050kg",
color:"white",
start: function(){
console.log("Car is going to start soon");
},
drive: function(){
console.log("The owner of the car start driving the car at 10pm.")
}
}
console.log(car.model);
car.start();
car.drive();
car ['name'] ="BMW";
console.log(car);
console.log(car["color"]);
console.log(typeof car);
//creating another object variable
const person = {
firstName: "John",
lastName: "Doe",
fullName: "John Doe",
age:50,
eyeColor:"blue",
UserId: function(){
console.log("Id: Shawn58");
},
UserPassword: function() {
console.log("Password: 221298");
}
};
console.log(person.firstName);
console.log(person.lastName);
console.log(person.fullName);
console.log(person.age);
console.log(typeof person.age);
person.UserId();
person.UserPassword();
console.log(typeof person);
/*console.log(beautifulCar);
//it'll not print result ,because
inside of function block variable cannot access from outside,for getting result we can just
call them by function name*/
let text ="abcdefgh"; //let's have a some practice about string
console.log(text.length);
//creating more string variables
let text1= "abc def gh";
console.log(text1.length);
{
let answer1 = "It's alright";
let answer2 = "He is called 'Johnny'";
let answer3 = 'He is called "Johnny"';
console.log(answer1);
console.log(typeof answer1);
console.log(answer1.length);
console.log(answer2);
console.log(typeof answer2);
console.log(answer2.length);
console.log(answer3);
console.log(typeof answer3);
console.log(answer3.length);
}
//Using escape character and and creating another variable
{
//let textOf = " We are the so-called "Vikings" from the north";
//this will print error, because of double quotes inside of the text and also outside of the text.
//There are two solution for this problem.1st one is,if you use double quotes outside then use single quotes inside.Another is using escape character.
//Creating another similar variable like above,and using escape character to solve this problem
let textOf = "We are the so-called \"Vikings\" from the north";
console.log(textOf);
console.log(typeof textOf);
/* All the Escape Character for solving this problem
Code Result Description
\' ' for single quotes
\" " for double quotes
\\ \ Backslash */
let textOfText ='We are the so-called \'Vinkings\' from the north ';
console.log(textOfText);
console.log(textOfText.length);
}
{
//Breaking a string
let lineBreak =" Hello \
Dolly";
console.log(lineBreak);
//Another thing, a safer way to break up a string,is to use string addition:
let textOfText = "Hello " +
"Dolly"; // It'll be the best practice for every programmers to use string addition instead of using backslash
console.log(textOfText);
//Last practice for line break
let line ="Hi there" + //So we know that ,the best practice for line break is using string addition
" I'm Shawn";
console.log(line);
/*We also know that if we use the "new" keyword in string
it will be become a object type data */
let x = "John"; //This one is string type variable
let y = new String("John"); //This one is object type data,because here we can find the "new" keyword.
console.log(typeof x);
console.log(typeof y);
console.log( x == y); //When using '==' operator, equal strings are equal(here only value equal).
//Never create string as object. It slows down execution speed.
//The "new" keyword complicates the code.
//This can produce some unexpected results
console.log( x === y );//This will be "false",because one is string and another is object type data
//Creating another two object type variable
let X = new String("John");
let Y = new String("John");
console.log(typeof X); //Let's check what type of data it is!
console.log(typeof Y); //Let's check what type of data it is!
//See the output in terminal,both of this object type data.
//Now see the comparison result
console.log( X === Y ); //Expected result is "true",but it will print "false".
//All the value is same,but this happened because "object can't be compared"
}
{
let x,y,z;
x = "Hi there,";
y = " I'm MD NIAMUL HAKIM SHAWN";
z = x + y;
console.log(z);
console.log(typeof z);
console.log(z.length);
}
{
//let text = "Welcome to my web page "hope" you will like my page"
//For the above one it will print error,for solving this problem we need to use "escape character"
let text = "Welcome to my web page \"hope\" you will like my page" //Now it will print expected result,because we used "Escape Character"
console.log(text);
console.log(text.length);
}
/*Extracting String Parts:
There are 3 methods for extracting a part of a String
1. slice(start, end)
2. substring(start, end)
3. substr(start, length)*/
{
//slice() method practice
//Already we know that slice() method can accept both positive and negative value
let text ="Apple, Banana, Blackberry";
//use positive value to declare Banana
console.log(text.slice(7,13));
//Now use negative value to declare Banana
console.log(text.slice(-18,-12));
//Create one more variable for practice slice() method
let foods ="Beef, Chicken, Fish";
console.log(foods); //check the full sting,then start working
console.log(foods.slice(6,13)); //positive values for extract "chicken".
console.log(foods.slice(-13,-6)); //Negative value for the same thing to cross check how it's work
//now take extract "Fish" from this string using positive values
console.log(foods.slice(15));// Here one value 15 is okay to extract fish.
// It's like if you want to extract "Chicken and Fish" you can apply this method
console.log(foods.slice(6));
//Here you don't need to mention the 'ending' point just need to mention starting point.Because you want to take all after starting point.
//You also can do this by using negative values
console.log(foods.slice(-13)); // Same value like the above one,result also "Chicken, Fish"
}
{
// NOw we're going to practice "substring()" method
// Create one variable for practice this method
// In substring() method we also need two value like slice() method,but we can use one value if we need to do that.
//"substring()" is similar to 'slice()', but the only difference is that "substring()" can't accept negative indexes.
let text ="Hello I'm Shawn, I'm studying CSE, at SWFU";
console.log(text); // Checking the string
console.log(text.substring(0,15)); // Taking the 1st part with positive index
console.log(text.substring(17,33)); // Taking the 2nd part,using positive index.
console.log(text.substring(35)); // Just one positive index because it's the last one, that's why it's similar to 'slice()'method,just can't use negative index.
// Now extract 2nd and 3rd part
console.log(text.substring(17));
// Let's create another variable for more practice of "substring()" method.
let foodValue= "Fish is good for health, Meat not good for body, Fish have no side affect but Meet have this.";
console.log(foodValue); // Check the full string first.
console.log(foodValue.substring(0,23)); // Taking the 1st part using positive index.
console.log(foodValue.substring(25,47)); // Taking the 2nd part.
console.log(foodValue.substring(49)); // Taking the 3rd part.
console.log(foodValue.substring(25)); // Taking the 2nd and 3rd part by using one positive index.
}
{
// Let's create another variable for practicing "substr()" method.
// In "substr()" we need 2 value ,first value for start and second value for length.
let foodCategory ="Fish for morning, Meat curry in the noon, both Fish and Meat in dinner";
console.log(foodCategory.substr(0,16)); //Here first value is starting point,and 2nd value for length(how long I want it'll be).
console.log(foodCategory.substr(18,22));// 1st value starting point,2nd value for length.
console.log(foodCategory.substr(42,30)); // 1st value for starting point,2nd value for length.
console.log(foodCategory.substr(18,52)); // Taking 1st value for starting from 2nd part,2nd value for the length of 2nd and 3rd part.
/*Create another variable for "substr()" method
final practice*/
let create = "string, array, object";
console.log(create.substr(0,6)); // Here 1st value for the starting point, and 2nd value for the length for the extracting text.
console.log(create.substr(8,5)); // Here also 1st value for the starting point and 2nd value for the length of the text.
console.log(create.substr(15,6)); // Same as above,1st value for the starting point and 2nd value for the length of the text.
//now extract the text from 2nd part to 3rd part
console.log(create.substr(8,13));
}
{
// Create some variable for practicing "replace()" method
let text = "Zahin loves Sansan";
let newText = text.replace("Sansan","Siri"); //1st parameter inside "replace()" indicating which one need to change, 2nd parameter is the replace value for that place.
let anotherText = newText.replace("Siri","Unknown");
// 2nd variable also same, 1s parameter inside "replace()"method indicating that,this word need to change.And 2nd parameter is the value for replace that word.
console.log(newText);
console.log(anotherText);
/* Note: "replace()" method does not change the string ,it is called on
It returns a new String.*/
}
{
//Creating another variable for practicing "replace()" method
/*Note: "replace()"method does not change the original String,
it's called on.
It returns a new String.
inside of "replace()"method,the 1st parameter indicate this word need to change,
and the 2nd parameter is the value for replacing word.*/
let configure = "Zahin is a good student";
let figureOut = configure.replace("good student","bad boy");
let conclusion = figureOut.replace("is a bad boy","have also some bad habit of drinking alchol and smoking weed.");
console.log(figureOut);
console.log(conclusion);
console.log(configure == figureOut);
console.log(typeof configure, typeof figureOut,typeof conclusion, figureOut == conclusion);
}
{
//Creating variables for practicing "toUpperCase()"string method.
let x ="Hi I'm Shawn";
let y = x.toUpperCase();
console.log(x);
console.log(y);
}
{
//Creating variable for practicing "toLowerCase()" string method.
let x ="Shawn is my nickname";
let y = x.toLowerCase();
console.log(x);
console.log(y);
}
//The concat() method
{
//"concat()"method can be used instead of using plus(+) operator
let x = "What to say?";
let y = " I don't know."
let z = x.concat("",y); //let z = x +""+y; (we can use one of them to add two string together)
console.log(z);
}
{
//Creating more variable for practice "concat()"string method.
let text1 = ": What to do now?";
let text2 = "I don't know, if you have any plan, you can tell me.";
let text3 = text1.concat("",text2); //We know that,we can do the same thing by using plus operator(+),there for, it'll be look like (let text3 = text1+""+text2;)like this.
console.log(typeof text3, text3.length, text3); //We can check lots of thing in one 'console.log()' statement,we have to separete them by commas.
}
// Important Note:
/*Note: All string methods return a new string. They don't modify the original string.
Formally said: String are immutable: String cannot be changed,only replaced.*/
{
//Last variable for practicing 'concat()'string method.
let shawn = "Shawn have 10000rmb, ";
let Zahin = "Zhain have 1000rmb,";
let TotalRmb = "Both of them have "+ shawn.concat("",Zahin)+" so we can say 11000rmb total.";
let both = shawn.concat("",Zahin);
console.log(both);
console.log(TotalRmb);
}
//"trim()"method in string
{
//creating variable for practicing "string.trim()"method
let x = " I'm here "
//check this with console
console.log(x);
//JavaScript count white blank,so we need to solve this problem with .trim()method.
//string.trim()method removes extra blank from string.
console.log(x.trim());
//Creating another variable for practicing "trim()"method
let y =" what's up guys?";
//check the console result first
console.log(y);
//now solve the white blank space problem.
console.log(y.trim());
}
//Again making another variable for practicing "trim()" method
{
let how = " Go to hell ";
console.log(how);
console.log(how.trim());
}
//Extracting string characters
/*Note: There are 3 methods for extracting string characters,
1. charAt()
2.charCodeAt()
3.Property access []*/
{
//creating variable for practicing "charAt()"string method
let x ="Hello World!"
console.log(x.charAt(0));
console.log(x.charAt(1));
console.log(x.charAt(2));
console.log(x.charAt(6));
}
{
//creating another variable for practicing"charAt()"string method
let y ="Hi,I'm Shawn";
console.log(y.charAt(3));
console.log(y.charAt(5));
console.log(y.charAt(7));
console.log(y.charAt(8));
console.log(y.charAt(9));
console.log(y.charAt(10));
console.log(y.charAt(11));
}
{
//Creating variable for practicing "charCodeAt()"string method
let x = "Hello";
console.log(x.charCodeAt(2));//I don't like to practice this too much because I don't like this,so I'm skiping this part
}
//Property Access
{
//Creating variable for practicing "property access []"
let s ="Shawn";
console.log(s[0]);
console.log(s[1]);
let r ="Revu";
console.log(r[0]);
console.log(r[1]);
}
//JavaScript String Search
/*JavaScript methods for searching strings:
1.String.indexOf()
2.String.lastIndexOf()
3.String.startsWith()
4.String.endsWith()
*/
{
//string.indexOf() method
//The 'indexOf()' method returns the index of (the position of) the 'first' occurrence of a specified text in a string.
let x = "Welcome to w3school JavaScript beatiful journy,I hope your JavaScript journy with us will be beautiful";
console.log(x.indexOf("Welcome"));
console.log(x.indexOf("w3school"));
console.log(x.indexOf("I"));
//string.lastIndexOf() method
// The 'lastIndexOf()' method returns the index of the 'last' occurrence of a specified text in a string
console.log(x.lastIndexOf("journy"));
console.log(x.lastIndexOf("JavaScript"));
console.log(x.lastIndexOf("beautiful"));
//Now have a look if the, If the text is not found.
console.log(x.indexOf("Python")); // I know, it will return -1,because the text is not found.
console.log(x.lastIndexOf("Whatever"));
// So the conclusion for both ".indexOf()" and ".lastIndexOf()" methods,if the text not found it will returns -1.
}
//Both of (".indexOf(),and .lastIndexOf()")accept 2nd parameter for where to search
{
//creating another string for practicing
let easy ="programmers practice:It's for only practice, you know nothing is important except practice regularly, because practice is the first priority for programmers";
//check ".indexOf()" method for searching strings
console.log(easy.indexOf("practice",8)); //Here 8 means,I order to ignore the 1st practice and start searching after this word
//But if I give the 2nd parameter 0(zero),then it will show the result 0(zero),because 'practice' is the first word of this string and also I'm looking for 'practice',
console.log(easy.indexOf("practice",0)); //string search methods are case sensitive,so becareful,if the string word exist in UpperCase and you search it in lower case it will show -1
//'-1' means the text is not found.
console.log(easy.lastIndexOf("programmers",12)); //By using 12, I order to ignore the last 'programmers'word...for the ".lastIndexOf()"method it start searching from end of the string to start point for the indicating text.
console.log(easy.lastIndexOf("practice"));
//I told you,".lastIndexOf()"method start searcing the text from the end of the string ,so it takes the last 'practice'
//And I told you before,if the text is not found both method it show "-1"
}
//Practicing "string.search()" method
{
let x = "Hello world,this world is beautiful,and simple to say as Hello";
console.log(x.search("Hello"));
console.log(x.search("beautiful"));
}
/*For the methods:
1. string.indexOf()
2. string.search()
Both of this methods looks like same but they're not same because:
a. The search() method cannot take a second starting position argument.
b. The indexOf()method cannot take powerful search values(regular expressions)
*/
{
//string.match()method
const text = "The rain in SPAIN stays mainly in the plain";
console.log(text.match(/ain/g))
/*If the regular expression does not include the g modifier (to perform a global search),
the match() method will return only the first match in the string.*/
console.log(text.match(/in/g));
//Returns: An Array, containing the matches, one item for each match, or null if no match is found.
//Now check,if the match not found it will return 'null'.
console.log(text.match(/zy/g)); //no 'zy' here ,so it will return "null"
//making one more string for practicing "string.match()"method
let y ="Hello there,I know him ,he is a good boy,he studying cse in our university,he always say to me hello there";
console.log(y.match(/he/g));
console.log(y.match(/there/g));
}
//Creating another string for practicing "string.includes()" method
{
// For "string.includes()" method, If the text found it will return value 'true'
// but if the text is not found then it will return value 'false'
let hello ="Nice to meet you";
console.log(hello.includes("nice"));
//It returns "false" because '.includes()'method case sensitive;
//so you need to write same case as it exist in string if you want 'true'
console.log(hello.includes("Nice"));
//creating more string for practice '.includes()' method
let x ="Hello JavaScript";
console.log(x.includes("JavaScript")); //case sensitive and it match to the exist text so it will return 'true'
console.log(x.includes("javascript")); //case sensitive and its not match to exist text so it will return 'false'
}
//Creating string for practicing "string.startsWith()" method
//'.startsWith()'method returns "true" if a string begins with a specified value,otherwise it'll returns "false"
{
//Creating a string for practicing "string.startsWith()"method
let x ="Hello world";
console.log(x.startsWith("Hello")); //It'll returns "true"
console.log(x.startsWith("Hello",5)); // It'll returns "false"
//Let's look at the back,check again previous string method chapter.
let y =" Welcome to JavaScript course ";
let z = x + y;
console.log(z);
console.log(z.replace("Hello world","JavaScript string method,"))//string.replace()method
console.log(z.slice(0,12));
console.log(z.slice(-18,-1));
console.log(z.substring(12,19));//both of .slice() and .substring() method similar,only the difference is .substring()method cannot accept negative value.
console.log(z.substr(20,2)); //for .substr() method the 1st parameter(20) for indicating the starting point,and 2nd parameter(2) for length of the text you want.
console.log(z.substr(0,11)) //for .substr() method the 1st parameter(0) for indicating the starting point,and 2nd parameter(11) for length of the text you want.
console.log(z.toUpperCase());
console.log(z.toLowerCase());
let a =" Destination is yours. "
console.log(a); //without string.trim() method
console.log(a.trim()); // string trim method will remove all the blank white space
//console.log(y.startsWith("Welcome"));
//console.log(y.startsWith("Welcome",7));
}
{
//Creating a string for practicing string search method.
/* {
JavaScript methods for searching strings:
1. String.indexOf()
2. String.lastIndexOf()
3. String.search()
4. String.includes()
5. String.startsWith()
6. String.endsWith()
}*/
let x ="Welcome to JavaScript w3school JavaScript lessons";
let y ="I'm learning JS now,JS easy to learn,learning this,it'll help me to";
// String.indexOf() method
console.log(x.indexOf("Welcome"));
//console.log(x.indexOf("w3school"));
console.log(x.indexOf("JavaScript",21)); //In ".indexOf()" method we can use 2nd parameter for indicating starting point if we want to use that.
//If the text is not exist then it will returns '-1'
console.log(x.indexOf("hi"))// There is no "hi" text in 'x',so it'll return '-1'
//".indexOf()" also case sensitive,the text in the string exist in 'UpperCase' but you write it in 'lowerCase',then it'll returns "-1" also
console.log(x.indexOf("welcome"));
//string.lastIndexOf() method
console.log(x.lastIndexOf("JavaScript",18));
//.lastIndexOf()method,we can use 2nd parameter for indicating the start point of searching the text in the string
//it's up to us,if we want then we can use 2nd parameter.If we don't use this ,there will no problem
//.lastIndexOf()method start finding the text from the end of the string
console.log(x.lastIndexOf("JavaScript"));
//It'll returns the last 'JavaScript' text,because this method finding the text from the end of string.
console.log(x.lastIndexOf("javascript"));
//.lastIndexOf()method also same as 'indexOf()'method,if the text is not found it'll returns '-1',here the problem is case sensitive that's why the text is not found.
//let x ="Welcome to JavaScript w3school JavaScript lessons";
//let y ="I'm learning JS now,JS easy to learn,learning this,it'll help me to";
//string.search() method
console.log(x.search("w3school"));
console.log(x.search("to"));
// The two methods, indexOf() and search(), are equal?
// The two methods are NOT equal. These are the differences:
// The search() method cannot take a second start position argument.
// The indexOf() method cannot take powerful search values (regular expressions).
/*String.match()
The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.
*/
console.log(y.match(/learn/g)); // It'll return all the matches result
console.log(y.match(/ea/gi));
// .includes() method
// The ".includes()"method returns true if a string contains a specified value
console.log(x.includes("Welcome"));//It'll returns 'true' because the text exist in the string
console.log(x.includes("welcome"));// It'll returns 'false' because the text exist in string upperCase but here I mentioned in lowerCase,".includes()"method is case sensitive
//We also can declare the start position of the indicating text by using 2nd parameter.
//".includes"method accept the 2nd parameter
console.log(x.includes("JavaScript",21));
//string.startsWith() method
//The "startsWith()" method returns 'true' if a string begins with a specified value, otherwise'false'
console.log(x.startsWith("Welcome")); // It'll returns 'true' because the string start with this text so the result will be true
console.log(x.startsWith("hello")); // It'll returns "false" because there is no text like this in x string.
console.log(typeof x,x.startsWith("welcome")); // It'll returns "false" also, because ".startWith()" method case sensitive
//also we can use 2nd parameter if we want to indicate the start position.
console.log(x.startsWith("to",8)); //It's returns 'true' because we mention the starting position,and the text'to'exist just after my indicating point,so returns "true"
//string.endsWith() method
//The "endsWith()" method returns "true" if a string ends with a specified value, otherwise "false"
console.log(y.endsWith("learning",12));
//Here I order by 2nd parameter,just find the specified value in this length,and check in between this length the mention value is exist or not ,
//Here it exist so it returns 'true'
console.log(x.endsWith("lessons"));
// If we don't want to use the length,it'll be also okay,because it's optional,it's up to you
//If we don't use length, then it will take a look the whole string.And will check the string endsWith the specified value or not
console.log(x.endsWith("Lesson"));
//It'll returns 'false' because string.endsWith() method also case sensitive
}
{
function noob() {
let noob ="ki holo,baal flaow";
console.log(noob);
return "sov noob";
}
noob();
}
//JS String Templates
{
//Creating variable for practicing JS string templates
//JS Template Literals
/*Synonyms:
1. Template Literals,
2. Template String,
3. String Templates,
4. Back-Tics Syntax*/
//Template Literals use back-ticks(``) rather than the quotes("") to difine a string.
let firstName =`MD NIAMUL HAKIM`;
let lastName =`SHAWN`;
let FullName = `Full Name: ${firstName} ${lastName}`;
console.log(FullName);
}
//JavaScript numbers
//JS number is our fresh and important lesson
{
//JavaScript has only one type of number.Numbers can be written with of without decimals
{
let x =3.14;
let y =3;
let z =x+y;
console.log(z);
}
//Extra large or extra small number can be written with scientific(exponent)notation:
{
let x = 10e5
let y = 15e-5
let z = x + y;
console.log(x);
console.log(y);
console.log(z);
}
//JavaScript numbers are always stored as double precision floating point numbers,following international IEEE 754 standard.
//Precision
{
//Integers (numbers without a period or exponent notation) are accurate up to 15 digits.
let x = 999999999999999; // x will be 999999999999999,because Integers number are accurate up to 15 digits
let y = 9999999999999999; // y will be 10000000000000000,because Integers number are accurate up to 15 digits,after that JS cannot read it properly.
console.log(x);
console.log(y);
}
//The maximum number of decimals is 17,but floating point arithmetic is not alway 100% accurate:
{
let x = 0.2 + 0.1;
console.log(x); // x will be 0.30000000000000004
//But we know the result.It is simple answer o.3
//for solving this problem we can use multiply and divide
let y = (0.2*10 + 0.1*10)/10;
console.log(y); // x will be 0.3
}
{
//Create one more variable to practice The maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate:
let x = (0.2*10 + 0.1*10)/10;
let y = (0.2*10 + 0.1*10)/10;
let z = x + y;
console.log(z);
}
//Adding Numbers and Strings
{
//JavaScript uses th + operator for both addition and concatenation.
//Numbers are added and strings are concatenated.
// If you add two numbers, the result will be a number
{
let x = 10;
let y = 20;
let z = x + y; // The result will be a number.
console.log(z); //Result will be 30, because two numbers added
}
// If you add two Strings, the result will be a String concatenation:
{
let x ="10";
let y ="20";
let z =x + y; //The result will be a String.
console.log(z); // The result will be 1020,because two Strings Concatenation.
}
// If you add a number and a String,the result will be a String concatenation
{
let x = 10;
let y = "10";
let z = x + y;
console.log(z); // z will be 1010(a string)
console.log(x == y);
console.log(x === y);
}
// The JavaScript interpreter works from 'left to right'
{
let x = 10;
let y = 20;
let z = "30";
let result = x + y + z; // The result will be 3030,because JS interpreter from left to right.
// First it will add x and y,then the result of x,y will concatenate with z,because z is a string and we know the rules for string and(+)oparetor.
console.log(result);
}
//For the same example we will change the position of z,then see what happened
{
let z = "30";
let x = 10;
let y = 20;
let result = z+x+y; // Here the result will be 301020,
//Because we know JS interpreter works from left to right.
//Here z is a String and after that x and y are numbers,
//But JS will give the priority to String we know that .
console.log(result);
}
// Numeric Strings
{
// JavaScript strings can have numeric content
let x = 100; //x is a String
let y = "100"; // y is a String
let z = x + y;
console.log(z);
console.log(x == y);
console.log(x === y);
}
// If there are two number in two different String if we use JS operator except (+),it will try to convert strings to numbers in all numeric operations:
//And we know if we use (+) operator,it will concatenate both.
{
//let's try one example from w3school
let x = "100";
let y ="10";
let z = x/y;
console.log(z); // The result will be 10
// One more example
let a = "100";
let b = "10";
let c = a*b;
console.log(c); // The result will be 1000
// Another example for practice
let A = "100";
let B ="10";
let C = A-B;
console.log(C);
// Last example for (+) operator,it cannot convert JS string number,it will concatenate them
let X = "100";
let Y = "10";
let Z = X+Y;
console.log(Z); // The result will be 10010,because it concatenating both String,not converting
}
// NaN - Not a Number
{
// "Nan" is a JavaScript reseved word indicating that a is not a legal number.
// Trying to do arithmetic with a non-numeric string will result in "NaN"(Not a Number)
{
let x = 100 / "Apple";
console.log(x); //result will be "NaN"(Not a Number)
let y = 100;
let z = "Apple";
let nan = y/z; //result will be "Nan"
console.log(nan);
let A = 10;
let B = "Banana";
let C = A*B; //The multiflication result of A and B will be "NaN"(Not a Number)
// Because A is number and B is a string text,If B was a string number then it will show legal value.
console.log(C);
}
}
{
// "isNaN()" JS function
//You can use the global JavaScirpt function "isNaN()" to find out if a value is a not a number.
{
let x = 10;
let y = "Apple";
let z = x/y;
console.log(z); //This will show "NaN"
console.log(isNaN(x));
console.log(isNaN(y));
console.log(isNaN(z));
}
let a = 100;
let b = "Apple";
let c = a * b; //The result will be "NaN"
console.log(c);
console.log(isNaN(a));
console.log(isNaN(b));