Skip to content

Commit 226afc0

Browse files
committed
added chapter 01
1 parent fc3176f commit 226afc0

14 files changed

+280
-0
lines changed

chapter01/01-HelloWorld.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="01-HelloWorld.js"></script>
9+
<script type="text/javascript">
10+
/*alert('Hello, World!');
11+
console.log('Hello, World!');*/
12+
</script>
13+
</body>
14+
</html>

chapter01/01-HelloWorld.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function output(t) {
2+
document.write('<p>' + t + '</p>');
3+
return;
4+
}
5+
6+
alert('Hello, World!');
7+
console.log('Hello, World!');
8+
output('Hello, World!');

chapter01/02-Variables.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="02-Variables.js"></script>
9+
</body>
10+
</html>

chapter01/02-Variables.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var num = 1; //{1}
2+
num = 3; //{2}
3+
4+
var price = 1.5; //{3}
5+
var name = 'Packt'; //{4}
6+
var trueValue = true; //{5}
7+
var nullVar = null; //{6}
8+
var und; //7
9+
10+
console.log("num: "+ num);
11+
console.log("name: "+ num);
12+
console.log("trueValue: "+ num);
13+
console.log("price: "+ num);
14+
console.log("nullVar: "+ nullVar);
15+
console.log("und: "+ und);

chapter01/03-Operators.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="03-Operators.js"></script>
9+
</body>
10+
</html>

chapter01/03-Operators.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Arithmetic operators */
2+
var num = 0;
3+
4+
console.log('num value is ' + num);
5+
6+
num = num + 2;
7+
8+
console.log('New num value is ' + num);
9+
10+
num = num * 3;
11+
12+
console.log('New num value is ' + num);
13+
14+
num = num / 2;
15+
16+
console.log('New num value is ' + num);
17+
18+
num++;
19+
20+
num--;
21+
22+
console.log('New num value is ' + num);
23+
24+
console.log('num mod 2 value is ' + (num % 2));
25+
26+
27+
/* Assignment operators */
28+
num += 1;
29+
num -= 2;
30+
num *= 3;
31+
num /= 2;
32+
num %= 3;
33+
34+
console.log('New num value is ' + num);
35+
36+
/* Assignment operators */
37+
console.log('num == 1 : ' + (num == 1));
38+
console.log('num === 1 : ' + (num === 1));
39+
console.log('num != 1 : ' + (num != 1));
40+
console.log('num > 1 : ' + (num > 1));
41+
console.log('num < 1 : ' + (num < 1));
42+
console.log('num >= 1 : ' + (num >= 1));
43+
console.log('num <= 1 : ' + (num <= 1));
44+
45+
46+
/* Logical operators */
47+
console.log('true && false : ' + (true && false));
48+
console.log('true || false : ' + (true || false));
49+
console.log('!true : ' + (!true));
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="04-ConditionalStatements.js"></script>
9+
</body>
10+
</html>

chapter01/04-ConditionalStatements.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* Example 01 - if */
2+
var num = 1;
3+
if (num == 1) {
4+
console.log("num is equal to 1");
5+
}
6+
7+
/* Example 02 - if-else */
8+
var num = 0;
9+
if (num == 1) {
10+
console.log("num is equal to 1");
11+
} else {
12+
console.log("num is not equal to 1, the value of num is " + num);
13+
}
14+
15+
/* Example 03 - if-else-if-else... */
16+
var month = 5;
17+
if (month == 1) {
18+
console.log("January");
19+
} else if (month == 2){
20+
console.log("February");
21+
} else if (month == 3){
22+
console.log("March");
23+
} else {
24+
console.log("Month is not January, February or March");
25+
}
26+
27+
/* Example 04 - switch */
28+
var month = 5;
29+
switch(month) {
30+
case 1:
31+
console.log("January");
32+
break;
33+
case 2:
34+
console.log("February");
35+
break;
36+
case 3:
37+
console.log("March");
38+
break;
39+
default:
40+
console.log("Month is not January, February or March");
41+
}

chapter01/05-Loops.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="05-Loops.js"></script>
9+
</body>
10+
</html>

chapter01/05-Loops.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
console.log('**** for example ****');
2+
3+
/* for - example */
4+
for (var i=0; i<10; i++) {
5+
console.log(i);
6+
}
7+
8+
console.log('**** while example ****');
9+
/* while - example */
10+
var i = 0;
11+
while(i<10)
12+
{
13+
console.log(i);
14+
i++;
15+
}
16+
17+
console.log('**** do-while example ****');
18+
/* do-while - example */
19+
var i = 0;
20+
do {
21+
console.log(i);
22+
i++;
23+
} while (i<10)

chapter01/06-Functions.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="06-Functions.js"></script>
9+
</body>
10+
</html>

chapter01/06-Functions.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function sayHello() {
2+
console.log('Hello!');
3+
}
4+
5+
sayHello();
6+
7+
/* function with parameter */
8+
function output(text) {
9+
console.log(text);
10+
}
11+
12+
output('Hello!');
13+
14+
output('Hello!', 'Other text');
15+
16+
output();
17+
18+
/* function using the return statement */
19+
function sum(num1, num2) {
20+
return num1 + num2;
21+
}
22+
23+
var result = sum(1,2);
24+
output(result);
25+
26+
27+
28+

chapter01/07-ObjectOrientedJS.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="07-ObjectOrientedJS.js"></script>
9+
</body>
10+
</html>

chapter01/07-ObjectOrientedJS.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* Object example 1 */
2+
3+
var obj = new Object();
4+
5+
/* Object example 2 */
6+
7+
var obj = {};
8+
9+
obj = {
10+
name: {
11+
first: 'Gandalf',
12+
last: 'the Grey'
13+
},
14+
address: 'Middle Earth'
15+
};
16+
17+
/* Object example 3 */
18+
19+
function Book(title, pages, isbn){
20+
this.title = title;
21+
this.pages = pages;
22+
this.isbn = isbn;
23+
this.printIsbn = function(){
24+
console.log(this.isbn);
25+
}
26+
}
27+
28+
var book = new Book('title', 'pag', 'isbn');
29+
30+
console.log(book.title); //outputs the book title
31+
32+
book.title = 'new title'; //update the value of the book title
33+
34+
console.log(book.title); //outputs the updated value
35+
36+
Book.prototype.printTitle = function(){
37+
console.log(this.title);
38+
};
39+
40+
book.printTitle();
41+
42+
book.printIsbn();

0 commit comments

Comments
 (0)