Skip to content

Commit c63f7c8

Browse files
committed
merged 2nd ed
1 parent 7e2e647 commit c63f7c8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1355
-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/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

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
/* Logical operators */
46+
console.log('true && false : ' + (true && false));
47+
console.log('true || false : ' + (true || false));
48+
console.log('!true : ' + (!true));
49+
50+
/* Bitwise operators */
51+
console.log('5 & 1:', (5 & 1)); //same as 0101 & 0001 (result 0001 / 1)
52+
console.log('5 | 1:', (5 | 1)); //same as 0101 | 0001 (result 0101 / 5)
53+
console.log('~ 5:', (~5)); //same as ~0101 (result 1010 / 10)
54+
console.log('5 ^ 1:', (5 ^ 1)); //same as 0101 ^ 0001 (result 0100 / 4)
55+
console.log('5 << 1:', (5 << 1)); //same as 0101 << 1 (result 1010 / 10)
56+
console.log('5 >> 1:', (5 >> 1)); //same as 0101 >> 1 (result 0010 / 2)
57+
58+
/* typeOf */
59+
console.log('typeof num:', typeof num);
60+
console.log('typeof Packt:', typeof 'Packt');
61+
console.log('typeof true:', typeof true);
62+
console.log('typeof [1,2,3]:', typeof [1,2,3]);
63+
console.log('typeof {name:John}:', typeof {name:'John'});
64+
65+
/* delete */
66+
var myObj = {name: 'John', age: 21};
67+
delete myObj.age;
68+
console.log(myObj); //Object {name: "John"}

chapter01/04-TruthyFalsy.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="04-TruthyFalsy.js"></script>
9+
</body>
10+
</html>

chapter01/04-TruthyFalsy.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function testTruthy(val){
2+
return val ? console.log('truthy') : console.log('falsy');
3+
}
4+
5+
testTruthy(true); //true
6+
testTruthy(false); //false
7+
testTruthy(new Boolean(false)); //true (object is always true)
8+
9+
testTruthy(''); //false
10+
testTruthy('Packt'); //true
11+
testTruthy(new String('')); //true (object is always true)
12+
13+
testTruthy(1); //true
14+
testTruthy(-1); //true
15+
testTruthy(NaN); //false
16+
testTruthy(new Number(NaN)); //true (object is always true)
17+
18+
testTruthy({}); //true (object is always true)
19+
20+
var obj = {name:'John'};
21+
testTruthy(obj); //true
22+
testTruthy(obj.name); //true
23+
testTruthy(obj.age); //age (prop does not exist)

chapter01/05-EqualsOperators.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-EqualsOperators.js"></script>
9+
</body>
10+
</html>

chapter01/05-EqualsOperators.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//Packt == true
2+
3+
console.log('packt' ? true : false);
4+
//outputs true
5+
6+
console.log('packt' == true);
7+
//1 - converts Boolean using toNumber
8+
//'packt' == 1
9+
//2 - converts String using toNumber
10+
//NaN == 1
11+
//outputs false
12+
13+
console.log('packt' == false);
14+
//1 - converts Boolean using toNumber
15+
//'packt' == 0
16+
//2 - converts String using toNumber
17+
//NaN == 0
18+
//outputs false
19+
20+
console.log([0] == true);
21+
//1 - converts Boolean using toNumber
22+
//[0] == 1
23+
//2 - converts Object using toPrimitive
24+
//2.1 - [0].valueOf() is not primitive
25+
//2.2 - [0].toString is 0
26+
//0 == 1
27+
//outputs false
28+
29+
//******************************* ===
30+
console.log('packt' === true); //false
31+
32+
console.log('packt' === 'packt'); //true
33+
34+
var person1 = {name:'John'};
35+
var person2 = {name:'John'};
36+
console.log(person1 === person2); //false, different objects
+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-ConditionalStatements.js"></script>
9+
</body>
10+
</html>

chapter01/06-ConditionalStatements.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}
42+
43+
/* Example 05 - ternary operator - if..else */
44+
if (num === 1){
45+
num--;
46+
} else {
47+
num++;
48+
}
49+
50+
//is the same as
51+
(num === 1) ? num-- : num++;

chapter01/07-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="07-Loops.js"></script>
9+
</body>
10+
</html>

chapter01/07-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/08-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="08-Functions.js"></script>
9+
</body>
10+
</html>

chapter02/01-Introduction.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="01-Introduction.js"></script>
9+
</body>
10+
</html>

chapter02/01-Introduction.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var averageTempJan = 31.9;
2+
var averageTempFeb = 35.3;
3+
var averageTempMar = 42.4;
4+
var averageTempApr = 52;
5+
var averageTempMay = 60.8;
6+
7+
var averageTemp = [];
8+
averageTemp[0] = 31.9;
9+
averageTemp[1] = 35.3;
10+
averageTemp[2] = 42.4;
11+
averageTemp[3] = 52;
12+
averageTemp[4] = 60.8;
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-CreatingAndInitialingArrays.js"></script>
9+
</body>
10+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var daysOfWeek = [];
2+
3+
var daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
4+
5+
var daysOfWeek = new Array(); //{1}
6+
7+
var daysOfWeek = new Array(7); //{2}
8+
9+
console.log(daysOfWeek.length);
10+
11+
var daysOfWeek = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'); //{3}
12+
13+
for (var i=0; i<daysOfWeek.length; i++){
14+
console.log(daysOfWeek[i]);
15+
}
16+
17+
//console.table(daysOfWeek);
18+
19+
//fibonacci numbers
20+
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
21+
var fibonacci = []; //{1}
22+
fibonacci[1] = 1; //{2}
23+
fibonacci[2] = 1; //{3}
24+
25+
for(var i = 3; i < 20; i++){
26+
fibonacci[i] = fibonacci[i-1] + fibonacci[i-2]; ////{4}
27+
}
28+
29+
for(var i = 1; i<fibonacci.length; i++){ //{5}
30+
console.log(fibonacci[i]); //{6}
31+
}
32+
33+
//instead of {5} and {6} we can simply use
34+
console.log(fibonacci);

0 commit comments

Comments
 (0)