8000 javascript-fundamentals-part-2 Β· sanketsangi/SimpleJavaScript@ecfed8c Β· GitHub
[go: up one dir, main page]

Skip to content

Commit ecfed8c

Browse files
committed
javascript-fundamentals-part-2
1 parent aea0ea0 commit ecfed8c

File tree

1 file changed

+294
-0
lines changed

1 file changed

+294
-0
lines changed

β€ŽREADME.md

+294
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ programming) styles.
4545
- [Basic Arrays Operations Methods](#basic-arrays-operations-methods)
4646
- [Introduction to Objects](#introduction-to-objects)
4747
- [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)
4853

4954

5055
----
@@ -887,4 +892,293 @@ console.log(john);
887892
// job: "teacher"
888893
// ​
889894
// 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+
8901184
```

0 commit comments

Comments
Β (0)
0