@@ -45,6 +45,11 @@ programming) styles.
45
45
- [ Basic Arrays Operations Methods] ( #basic-arrays-operations-methods )
46
46
- [ Introduction to Objects] ( #introduction-to-objects )
47
47
- [ Dot vs Braket Notation] ( #dot-vs-braket-notation )
48
+ - [ Object Methods] ( #object-methods )
49
+ - [ Iteration: The For Loop] ( #iteration-the-for-loop )
50
+ - [ Looping Array, Breaking and Continuing] ( #looping-array-breaking-and-continuing )
51
+ - [ Looping Backwards and Loops in Loops] ( #looping-backwards-and-loops-in-loops )
52
+ - [ The While Loop] ( #the-while-loop )
48
53
49
54
50
55
----
@@ -887,4 +892,293 @@ console.log(john);
887
892
// job: "teacher"
888
893
// β
889
894
// lastName: "Smith"
895
+
896
+ ```
897
+
898
+
899
+ ## Object Methods
900
+
901
+ ``` jsx
902
+
903
+ const john = {
904
+ firstName: ' John' ,
905
+ lastName: ' Smith' ,
906
+ birthYear: 1991 ,
907
+ job: ' teacher' ,
908
+ favoriteFruit: [' apple' , ' orange' , ' banana' ],
909
+ hasDrivingLicense: true ,
910
+
911
+ calcAge : function () {
912
+ console .log (this );
913
+ return 2037 - this .birthYear ;
914
+ },
915
+
916
+ getSummary : function () {
917
+ return ` ${ this .firstName } is a ${ this .calcAge ()} -years old ${ john .job } , and he has ${ this .hasDriverLicense ? ' a' : ' no' } driver license` ;
918
+ }
919
+ };
920
+
921
+ console .log (john .calcAge ());
922
+ console .log (john .getSummary ());
923
+
924
+
925
+ // {
926
+ // firstName: 'John',
927
+ // lastName: 'Smith',
928
+ // birthYear: 1991,
929
+ // job: 'teacher',
930
+ // favoriteFruit: [ 'apple', 'orange', 'banana' ],
931
+ // hasDrivingLicense: true,
932
+ // calcAge: Ζ calcAge(),
933
+ // getSummary: Ζ getSummary()
934
+ // }
935
+ // {
936
+ // firstName: 'John',
937
+ // lastName: 'Smith',
938
+ // birthYear: 1991,
939
+ // job: 'teacher',
940
+ // favoriteFruit: [ 'apple', 'orange', 'banana' ],
941
+ // hasDrivingLicense: true,
942
+ // calcAge: Ζ calcAge(),
943
+ // getSummary: Ζ getSummary()
944
+ // }
945
+ // 46
946
+ // 'John is a 46 -years old teacher, and he has no driver license'
947
+
948
+
949
+ ```
950
+
951
+
952
+ ## Iteration: The For Loop
953
+
954
+ ``` jsx
955
+
956
+ for (let rep = 1 ; rep <= 30 ; rep++ ) {
957
+ console .log (` Lifting weights repetition ${ rep} ππ»ββοΈ` );
958
+ }
959
+
960
+
961
+ // 'Lifting weights repetition 1ππ»ββοΈ'
962
+ // 'Lifting weights repetition 2ππ»ββοΈ'
963
+ // 'Lifting weights repetition 3ππ»ββοΈ'
964
+ // 'Lifting weights repetition 4ππ»ββοΈ'
965
+ // 'Lifting weights repetition 5ππ»ββοΈ'
966
+ // 'Lifting weights repetition 6ππ»ββοΈ'
967
+ // 'Lifting weights repetition 7ππ»ββοΈ'
968
+ // 'Lifting weights repetition 8ππ»ββοΈ'
969
+ // 'Lifting weights repetition 9ππ»ββοΈ'
970
+ // 'Lifting weights repetition 10ππ»ββοΈ'
971
+ // 'Lifting weights repetition 11ππ»ββοΈ'
972
+ // 'Lifting weights repetition 12ππ»ββοΈ'
973
+ // 'Lifting weights repetition 13ππ»ββοΈ'
974
+ // 'Lifting weights repetition 14ππ»ββοΈ'
975
+ // 'Lifting weights repetition 15ππ»ββοΈ'
976
+ // 'Lifting weights repetition 16ππ»ββοΈ'
977
+ // 'Lifting weights repetition 17ππ»ββοΈ'
978
+ // 'Lifting weights repetition 18ππ»ββοΈ'
979
+ // 'Lifting weights repetition 19ππ»ββοΈ'
980
+ // 'Lifting weights repetition 20ππ»ββοΈ'
981
+ // 'Lifting weights repetition 21ππ»ββοΈ'
982
+ // 'Lifting weights repetition 22ππ»ββοΈ'
983
+ // 'Lifting weights repetition 23ππ»ββοΈ'
984
+ // 'Lifting weights repetition 24ππ»ββοΈ'
985
+ // 'Lifting weights repetition 25ππ»ββοΈ'
986
+ // 'Lifting weights repetition 26ππ»ββοΈ'
987
+ // 'Lifting weights repetition 27ππ»ββοΈ'
988
+ // 'Lifting weights repetition 28ππ»ββοΈ'
989
+ // 'Lifting weights repetition 29ππ»ββοΈ'
990
+ // 'Lifting weights repetition 30ππ»ββοΈ'
991
+
992
+ ```
993
+
994
+
995
+ ## Looping Array, Breaking and Continuing
996
+
997
+ ``` jsx
998
+
999
+ const john = [
1000
+ ' John' ,
1001
+ ' Smith' ,
1002
+ ' 2037 - 1991' ,
1003
+ ' teacher' ,
1004
+ [' apple' , ' orange' , ' banana' ]
1005
+ ];
1006
+ const types = [];
1007
+
1008
+ for (let i = 0 ; i < john .length ; i++ ) {
1009
+ console .log (john[i], typeof john[i]);
1010
+ // Reading from john array
1011
+ types[i] = typeof john[i];
1012
+
1013
+ }
1014
+
1015
+ console .log (types);
1016
+
1017
+ const years = [1991 , 2007 , 1969 , 2020 ];
1018
+ const ages = [];
1019
+
1020
+ for (let i = 0 ; i < years .length ; i++ ) {
1021
+ // Filling types array
1022
+ ages .push (2037 - years[i]);
1023
+ }
1024
+
1025
+ console .log (ages);
1026
+
1027
+ // continue and break
1028
+ console .log (' --- ONLY STRINGS ---' );
1029
+ for (let i = 0 ; i < john .length ; i++ ) {
1030
+ if (typeof john[i] !== ' string' ) continue ;
1031
+
1032
+ console .log (john[i], typeof john[i]);
1033
+ }
1034
+
1035
+ console .log (' --- BREAK WITH NUMBER ---' );
1036
+ for (let i = 0 ; i < john .length ; i++ ) {
1037
+ if (typeof john[i] === ' number' ) break ;
1038
+
1039
+ console .log (john[i], typeof john[i]);
1040
+ }
1041
+
1042
+ // 'John' 'string'
1043
+ // 'Smith' 'string'
1044
+ // '2037 - 1991' 'string'
1045
+ // 'teacher' 'string'
1046
+ // 'object'
1047
+ // [ 'apple', 'orange', 'banana' ] 'object'
1048
+ // [ 'string', 'string', 'string', 'string', 'object' ]
1049
+
1050
+
1051
+
1052
+
1053
+
1054
+
1055
+
1056
+ // 4
1057
+
1058
+ // [ 46, 30, 68, 17 ]
1059
+
1060
+
1061
+ // '--- ONLY STRINGS ---'
1062
+
1063
+
1064
+
1065
+ // 'John' 'string'
1066
+ // 'Smith' 'string'
1067
+ // '2037 - 1991' 'string'
1068
+ // '--- BREAK WITH NUMBER ---'
1069
+ // 'teacher' 'string'
1070
+
1071
+
1072
+ // 'John' 'string'
1073
+ // 'Smith' 'string'
1074
+ // '2037 - 1991' 'string'
1075
+ // 'teacher' 'string'
1076
+ // [ 'apple', 'orange', 'banana' ] 'object'
1077
+
1078
+ ```
1079
+
1080
+ ## Looping Backwards and Loops in Loops
1081
+
1082
+ ``` jsx
1083
+
1084
+ const john = [
1085
+ ' John' ,
1086
+ ' Smith' ,
1087
+ ' 2037 - 1991' ,
1088
+ ' teacher' ,
1089
+ [' apple' , ' orange' , ' banana' ]
1090
+ ];
1091
+
1092
+ for (let i = john .length - 1 ; i >= 0 ; i-- ) {
1093
+ console .log (i, john[i]);
1094
+ }
1095
+
1096
+ for (let exercise = 1 ; exercise < 4 ; exercise++ ) {
1097
+ console .log (` --------- Starting exercise ${ exercise} --------` );
1098
+
1099
+ for (let rep = 1 ; rep < 6 ; rep++ ) {
1100
+ console .log (` Exercise ${ exercise} : Lifting weight repetition ${ rep} ππ»ββοΈ` );
1101
+ }
1102
+ }
1103
+
1104
+
1105
+ // 4 [ 'apple', 'orange', 'banana' ]
1106
+ // 3 'teacher'
1107
+ // 2 '2037 - 1991'
1108
+ // 1 'Smith'
1109
+ // '--------- Starting exercise 1 --------'
1110
+ // 0 'John'
1111
+ // '--------- Starting exercise 2 --------'
1112
+ // 'Exercise 1: Lifting weight repetition 1ππ»ββοΈ'
1113
+ // 'Exercise 1: Lifting weight repetition 2ππ»ββοΈ'
1114
+ // 'Exercise 1: Lifting weight repetition 3ππ»ββοΈ'
1115
+ // 'Exercise 1: Lifting weight repetition 4ππ»ββοΈ'
1116
+ // 'Exercise 1: Lifting weight repetition 5ππ»ββοΈ'
1117
+ // 'Exercise 2: Lifting weight repetition 1ππ»ββοΈ'
1118
+ // 'Exercise 2: Lifting weight repetition 2ππ»ββοΈ'
1119
+ // 'Exercise 2: Lifting weight repetition 3ππ»ββοΈ'
1120
+ // 'Exercise 2: Lifting weight repetition 4ππ»ββοΈ'
1121
+ // 'Exercise 2: Lifting weight repetition 5ππ»ββοΈ'
1122
+ // '--------- Starting exercise 3 --------'
1123
+ // 'Exercise 3: Lifting weight repetition 1ππ»ββοΈ'
1124
+ // 'Exercise 3: Lifting weight repetition 2ππ»ββοΈ'
1125
+ // 'Exercise 3: Lifting weight repetition 3ππ»ββοΈ'
1126
+ // 'Exercise 3: Lifting weight repetition 4ππ»ββοΈ'
1127
+ // 'Exercise 3: Lifting weight repetition 5ππ»ββοΈ'
1128
+
1129
+ ```
1130
+
1131
+
1132
+ ## The While Loop
1133
+
1134
+ ``` jsx
1135
+
1136
+ for (let rep = 1 ; rep <= 10 ; rep++ ) {
1137
+ console .log (` Lifting weights repetition ${ rep} ππ»ββοΈ` );
1138
+ }
1139
+
1140
+ let rep = 1 ;
1141
+ while (rep <= 10 ) {
1142
+ console .log (` Lifting weights repetition ${ rep} ππ»ββοΈ` );
1143
+ rep++ ;
1144
+ }
1145
+
1146
+
1147
+ // 'Lifting weights repetition 1ππ»ββοΈ'
1148
+ // 'Lifting weights repetition 2ππ»ββοΈ'
1149
+ // 'Lifting weights repetition 3ππ»ββοΈ'
1150
+ // 'Lifting weights repetition 4ππ»ββοΈ'
1151
+ // 'Lifting weights repetition 5ππ»ββοΈ'
1152
+ // 'Lifting weights repetition 1ππ»ββοΈ'
1153
+ // 'Lifting weights repetition 6ππ»ββοΈ'
1154
+ // 10
1155
+ // 'Lifting weights repetition 7ππ»ββοΈ'
1156
+ // 'Lifting weights repetition 8ππ»ββοΈ'
1157
+ // 'Lifting weights repetition 9ππ»ββοΈ'
1158
+ // 'Lifting weights repetition 10ππ»ββοΈ'
1159
+ // 'Lifting weights repetition 2ππ»ββοΈ'
1160
+ // 'Lifting weights repetition 3ππ»ββοΈ'
1161
+ // 'Lifting weights repetition 4ππ»ββοΈ'
1162
+ // 'Lifting weights repetition 5ππ»ββοΈ'
1163
+ // 'Lifting weights repetition 6ππ»ββοΈ'
1164
+ // 'Lifting weights repetition 7ππ»ββοΈ'
1165
+ // 'Lifting weights repetition 8ππ»ββοΈ'
1166
+ // 'Lifting weights repetition 9ππ»ββοΈ'
1167
+ // 'Lifting weights repetition 10ππ»ββοΈ'
1168
+
1169
+ let rep = 1 ;
1170
+ while (rep <= 10 ) {
1171
+ rep++ ;
1172
+ }
1173
+
1174
+ let dice = Math .trunc (Math .random () * 6 ) + 1 ;
1175
+
1176
+ while (dice !== 6 ) {
1177
+ console .log (` Rolling dice ${ dice} π²` );
1178
+ dice = Math .trunc (Math .random () * 6 ) + 1 ;
1179
+ if (dice === 6 ) console .log (' Loop is about to end...' );
1180
+ }
1181
+
1182
+ // 10
1183
+
890
1184
```
0 commit comments