Skip to content

Commit d4c49da

Browse files
committed
Merge branch 'master' into third-edition
# Conflicts: # .gitignore # chapter01/01-HelloWorld.js # chapter01/04-TruthyFalsy.js # chapter01/05-EqualsOperators.js # chapter01/06-ConditionalStatements.js # chapter01/14-ES6ParameterHandling.js # chapter02/01-Introduction.js # chapter02/02-CreatingAndInitialingArrays.js # chapter02/03-AddingRemovingElements.js # chapter02/04-TwoDimensionalMultiDimensional.js # chapter02/05-ArrayMethods.js # chapter03/03-BalancedSymbols.html # chapter03/04-DecimalToBinary.html # chapter03/04-DecimalToBinary.js # chapter03/05-TowerOfHanoi.html # chapter04/03-PriorityQueue.html # chapter04/04-HotPotato.html # chapter06/03-Operations.html # chapter07/02-UsingDictionaries.html # chapter07/03-HashTable.js # chapter07/04-UsingHash.html # chapter07/04-UsingHash.js # chapter07/06-UsingHashCollisionSeparateChaining.js # chapter07/08-UsingHashCollisionLinearProbing.html # chapter08/01-BinarySearchTree.js # chapter09/02-UsingGraphs.html # chapter09/02-UsingGraphs.js # chapter11/01-Recursion.html # chapter11/01-Recursion.js # chapter11/02-InfiniteRecursion.html # chapter11/02-InfiniteRecursion.js # chapter11/03-MinCoinChangeDP.html # chapter11/04-MinCoinChangeGreedy.html
2 parents ef79e85 + 208b757 commit d4c49da

Some content is hidden

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

44 files changed

+1092
-0
lines changed

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/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.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

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/14-ES6ParameterHandling.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//******* EcmaScript 6: Default Parameter Values
2+
function sum (x = 1, y = 2, z = 3) {
3+
return x + y + z
4+
};
5+
console.log(sum(4,2)); //outpus 9
6+
7+
//function above is the same as
8+
function sum2 (x, y, z) {
9+
if (x === undefined)
10+
x = 1;
11+
if (y === undefined)
12+
y = 2;
13+
if (z === undefined)
14+
z = 3;
15+
return x + y + z;
16+
};
17+
console.log(sum2(4,2)); //outpus 9
18+
19+
//******* EcmaScript 6: spread operator ('...')
20+
var params = [3, 4, 5];
21+
console.log(sum(...params));
22+
23+
var numbers = [1, 2, ...params]; //pushing values into array
24+
console.log(numbers);
25+
26+
//******* EcmaScript 6: rest parameter ('...')
27+
function restParamaterFunction (x, y, ...a) {
28+
return (x + y) * a.length;
29+
}
30+
console.log(restParamaterFunction(1, 2, "hello", true, 7)); // outputs 9;
31+
32+
//code above is the same as ES5:
33+
function restParamaterFunction2 (x, y) {
34+
var a = Array.prototype.slice.call(arguments, 2);
35+
return (x + y) * a.length;
36+
};
37+
console.log(restParamaterFunction2(1, 2, "hello", true, 7));

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,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);
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
function printArray(myArray) {
2+
for (var i=0; i<myArray.length; i++){
3+
console.log(myArray[i]);
4+
}
5+
}
6+
7+
var numbers = [0,1,2,3,4,5,6,7,8,9];
8+
9+
//add a new element to the numbers array
10+
numbers[numbers.length] = 10;
11+
12+
numbers.push(11);
13+
14+
numbers.push(12, 13);
15+
16+
//printArray(numbers);
17+
18+
//insert first position manually
19+
for (var i=numbers.length; i>=0; i--){
20+
numbers[i] = numbers[i-1];
21+
}
22+
23+
numbers[0] = -1;
24+
25+
//printArray(numbers);
26+
27+
//using method unshift
28+
numbers.unshift(-2);
29+
30+
//printArray(numbers);
31+
32+
numbers.unshift(-4, -3);
33+
34+
//printArray(numbers);
35+
36+
//**** Removing elements
37+
38+
numbers.pop();
39+
40+
//remove first position manually
41+
/*for (var i=0; i<numbers.length; i++){
42+
numbers[i] = numbers[i+1];
43+
}*/
44+
45+
printArray(numbers);
46+
47+
console.log(numbers.length);
48+
49+
//using method shift
50+
numbers.shift();
51+
52+
printArray(numbers);
53+
console.log(numbers.length);
54+
55+
//**** Removing and Adding elements from the middle of the array or specific position
56+
//splice method - parameter (index, howManyPositionsToBeRemoved, item1...itemX)
57+
numbers.splice(5,3);
58+
59+
console.log('----');
60+
61+
printArray(numbers);
62+
63+
numbers.splice(5,0,2,3,4);
64+
65+
console.log('----');
66+
67+
printArray(numbers);
68+
69+
console.log('----');
70+
71+
numbers.splice(5,3,2,3,4);
72+
73+
printArray(numbers);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
var averageTempDay1 = [72,75,79,79,81,81];
2+
var averageTempDay2 = [81,79,75,75,73,72];
3+
4+
var averageTemp = [];
5+
6+
//same as
7+
averageTemp[0] = [72,75,79,79,81,81];
8+
averageTemp[1] = [81,79,75,75,73,72];
9+
10+
function printMatrix(myMatrix) {
11+
for (var i=0; i<myMatrix.length; i++){
12+
for (var j=0; j<myMatrix[i].length; j++){
13+
console.log(myMatrix[i][j]);
14+
}
15+
}
16+
}
17+
18+
printMatrix(averageTemp);
19+
20+
//same as
21+
22+
//day 1
23+
averageTemp[0] = [];
24+
averageTemp[0][0] = 72;
25+
averageTemp[0][1] = 75;
26+
averageTemp[0][2] = 79;
27+
averageTemp[0][3] = 79;
28+
averageTemp[0][4] = 81;
29+
averageTemp[0][5] = 81;
30+
//day 2
31+
averageTemp[1] = [];
32+
averageTemp[1][0] = 81;
33+
averageTemp[1][1] = 79;
34+
averageTemp[1][2] = 75;
35+
averageTemp[1][3] = 75;
36+
averageTemp[1][4] = 73;
37+
averageTemp[1][5] = 72;
38+
39+
printMatrix(averageTemp);
40+
41+
//** Multidimensional Matrix
42+
43+
//Matrix 3x3x3 - Cube
44+
45+
var matrix3x3x3 = [];
46+
for (var i=0; i<3; i++){
47+
matrix3x3x3[i] = [];
48+
for (var j=0; j<3; j++){
49+
matrix3x3x3[i][j] = [];
50+
for (var z=0; z<3; z++){
51+
matrix3x3x3[i][j][z] = i+j+z;
52+
}
53+
}
54+
}
55+
56+
for (var i=0; i<matrix3x3x3.length; i++){
57+
for (var j=0; j<matrix3x3x3[i].length; j++){
58+
for (var z=0; z<matrix3x3x3[i][j].length; z++){
59+
console.log(matrix3x3x3[i][j][z]);
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)