Skip to content

Commit 42b0a18

Browse files
committed
Merged the repos
2 parents a39fa72 + c6d03ee commit 42b0a18

39 files changed

+1090
-194
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
package-lock.json
33
docs/_sidebar.md
4+
.DS_Store

JavaScript_Advance/generators.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Generators are functions with the possibility of exit and subsequent entry.
3+
Their execution context (variable values) is preserved on subsequent inputs.
4+
*/
5+
6+
// Let's consider a simple example:
7+
function* myGenerator() {
8+
yield 5
9+
yield 6
10+
}
11+
12+
// In the example above, we wrote a simple generator function with asteriks(*) notation.
13+
// Next to the yield we put values that are going to be extracted from the function
14+
// In order to extract them one by one, we should at first call myGenerator function
15+
16+
const gen = myGenerator()
17+
18+
// The returning value of myGenerator function is a object-iterator. It has next() method
19+
// Which we may call to get the current generator function value:
20+
21+
gen.next().value // 5
22+
23+
// We get the value field of 'next' method's returning value, wich is an object as well
24+
// Let's call this again in order to get the next value:
25+
26+
gen.next().value // 6
27+
28+
// After we iterate through all the values, the next value is going to be undefined.
29+
30+
gen.next().value // undefined.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Getting values of environment variables
2+
// For example, getting username of machine
3+
const ENV_VARIABLE_KEY = "USER";
4+
5+
const ENV_VARIABLE_VALUE = process.env[ENV_VARIABLE_KEY];
6+
7+
console.log(ENV_VARIABLE_VALUE);

JavaScript_Basics/boolean.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
### What are boolean data types?
2+
3+
JavaScript **Boolean** is a data type which can store any one of two values, **true** or **false**.
4+
E.g. -
5+
6+
```
7+
// Javascript has a specific constructor for creating Boolean type objects
8+
const testValue = new Boolean(true);
9+
console.log(testValue); // it prints **Boolean {true}** since testValue is an Object of type Boolean
10+
```
11+
12+
We can directly assign boolean value to any variable, like below:
13+
14+
```
15+
const isTrue = true;
16+
const isfalse = false;
17+
```
18+
19+
In JavaScript, a **truthy** value is a value that translates to true when evaluated in a Boolean context.
20+
All values are truthy unless they are defined as **falsy**.
21+
22+
Javascript treats an empty string (**""**), **0**, **undefined** **null** and **NaN** as **falsy**.
23+
24+
Not operator or **!** is the operator which is used to **negate** the boolean aspect of a value.
25+
26+
For example:
27+
```
28+
const val = 12;
29+
// using ! operator
30+
console.log( !false ); // prints true
31+
console.log( !12 ); // prints false, since 12 is truthy value and negation of a truthy value is falsy
32+
// using !! operator - this returns just the boolean context of any value
33+
console.log( !!true ); // prints true
34+
console.log( !!false ); // prints false
35+
console.log( !!12 ); // prints true, since 12 is truthy value and negation of a truthy value is falsy, and again negation of that false value is truthy
36+
```

JavaScript_Basics/inheritance.js

Lines changed: 137 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,63 @@
1-
// common functions are combined in a class to have less repetition in code
1+
/*
2+
// ====================================
3+
// ====================================
4+
// BASE INHERITANCE EXAMPLE
5+
// ====================================
6+
// ====================================
7+
*/
28

3-
class Animals { // Animal class is created where the legs are defined 4
9+
let baseObject = {
10+
a: 1,
11+
b: 2
12+
}
13+
14+
// Create a new object with baseObject as the prototype for extendedObject.
15+
let extendedObject = Object.create(baseObject);
16+
17+
// Define property c to the baseObject.
18+
// This is not explicitly defined on extendedObject
19+
// but is accessible via the [[Prototype]].
20+
baseObject.c = 3;
21+
22+
// It appears that there are no properties on extendedObject
23+
console.log(extendedObject) // {}
24+
25+
// a and b were defined on the baseObject.
26+
// The property looks up the prototype chain
27+
// until the properties of a and b exist or null is encountered for [[Prototype]].
28+
console.log(extendedObject.a); // 1
29+
console.log(extendedObject.b); // 2
30+
31+
// Define d on the extendedObject
32+
// this is not accessible to the baseObject
33+
// as the [[Prototype]] inherits one way
34+
extendedObject.d = 4;
35+
36+
// We now see property d on extendedObject
37+
console.log(extendedObject) // { d: 4 }
38+
39+
// c is accessible because the prototype chain is "alive".
40+
// As properties are added or deleted, they can be accessed
41+
// by objects that share a prototype
42+
console.log(extendedObject.c); // 3
43+
44+
// d is accessible on extendedObject where it was defined
45+
// but is inaccessible to baseObject and returns undefined
46+
console.log(extendedObject.d); // 4
47+
console.log(baseObject.d) // undefined
48+
49+
50+
/*
51+
// ====================================
52+
// ====================================
53+
// CLASS BASED INHERITANCE EXAMPLE
54+
// ====================================
55+
// ====================================
56+
*/
57+
58+
// Animal class is defined with a property of legs and a method of getLegs
59+
// which returns the current value of legs.
60+
class Animal {
461
constructor () {
562
this.legs = 4;
663
}
@@ -10,26 +67,97 @@ class Animals { // Animal class is created where the legs are defined 4
1067
}
1168
}
1269

13-
class Dog extends Animals {
70+
// Dog inherits the base properties from Animal
71+
// and adds its own property of age (which is not shared with Animal)
72+
// and the methods getAge and getSound
73+
class Dog extends Animal {
1474
constructor (age) {
15-
super(); // As We have to also call the constructor of Animals if constructor is empty this automatically calls the super
75+
76+
// We need to call super in order to instantiate the constructor on Animal.
77+
// If super is not called, then legs would be inaccessible to the Dog class
78+
super();
1679
this.age = age;
1780
}
1881

19-
// we have inherited the animals so getLegs method is also there in Dog class
2082
getSound () {
21-
return ("Bhow");
83+
return ("Bow");
2284
}
2385

2486
getAge () {
2587
return (this.age);
2688
}
2789
}
2890

29-
const tommy = new Dog(10); // Created Object of Dog class
91+
// Create a new Dog object passing in 10 for the age parameter
92+
const tommy = new Dog(10);
3093

94+
// Since the Dog class extends the Animal class
95+
// tommy has access to the getLegs method
3196
console.log(tommy.getLegs()); // Output 4
3297

33-
console.log(tommy.getSound()); // Bhow
98+
console.log(tommy.getSound()); // Bow
99+
100+
console.log(tommy.getAge()); // 10
101+
102+
103+
/*
104+
// ====================================
105+
// ====================================
106+
// Function Based Inheritance Example
107+
// ====================================
108+
// ====================================
109+
*/
110+
111+
// This function defines the base "constructor" for us and is
112+
// comparable to the constructor function found in the class example
113+
function Animal() {
114+
this.legs = 4;
115+
}
116+
117+
// Adding the getLegs property to the prototype ensures that
118+
// it will be inherited to any objects that are created from Animal
119+
Animal.prototype.getLegs = function() {
120+
return this.legs;
121+
}
122+
123+
// The Dog function acts as the constructor, but where is the super call
124+
// that we use to call the constructor from Animal?
125+
function Dog(age) {
126+
// the call method is similar in function to calling the super method in the
127+
// super method in the class based example. In this case, it instantiates the
128+
// legs property
129+
Animal.call(this);
130+
131+
this.age = age;
132+
}
133+
134+
// We set the prototype of Dog to the prototype of Animal
135+
// in order to have access to the getLegs method that is
136+
// defined on Animal's prototype
137+
Dog.prototype = Object.create(Animal.prototype);
138+
139+
// Dog is set as the constructor property on the Dog prototype to
140+
// match the object structure of the class example
141+
Dog.prototype.constructor = Dog;
142+
143+
// Define getSound method
144+
Dog.prototype.getSound = function() {
145+
return ("Bow");
146+
}
147+
148+
// Define getAge method
149+
Dog.prototype.getAge = function() {
150+
return (this.age);
151+
}
152+
153+
// create new Dog with age 10
154+
const tommy = new Dog(10);
155+
156+
console.log(tommy); // Dog { legs: 4, age: 10 }
157+
158+
// call getLegs that was inherited from Animal
159+
console.log(tommy.getLegs()); // 4
34160

35-
console.log(tommy.getAge()); // 10
161+
// call both getSound and getAge that are defined on Dog
162+
console.log(tommy.getSound()); // Bow
163+
console.log(tommy.getAge()); // 10

JavaScript_Basics/math.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ Math.min(50, 50, 810, 2200, -900); // will return -900
1313
Math.max(230, 250, 10, 300, -900); // will return 300
1414

1515
// round function
16-
Math.random(); // returns a random number
16+
Math.round(5.899);
17+
18+
// returns a random number that is not an integer between 1 to 10.
19+
Math.random() * (10 - 1) + 1;
1720

1821
// Defining variables to carry out the mathematical functions on.
1922
value1 = 10;

0 commit comments

Comments
 (0)