forked from Apress/js-data-structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter2(UniqueParts).js
148 lines (109 loc) · 2.65 KB
/
Chapter2(UniqueParts).js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
test = "sss";
console.log(test); // prints "sss"
function scope1(){
var top = "top";
bottom = "bottom";
console.log(bottom);
var bottom;
}
scope1(); // prints "bottom" - no error
function scope1(){
var top = "top";
var bottom;
bottom = "bottom";
console.log(bottom);
}
scope1(); // prints "bottom" - no error
function scope2(print) {
if (print) {
var insideIf = '12';
}
console.log(insideIf);
}
scope2(true); // prints '12' - no error
function scope2(print) {
var insideIf;
if (print) {
insideIf = '12';
}
console.log(insideIf);
}
scope2(true); // prints '12' - no error
var a = 1;
function four() {
if (true) {
var a = 4;
}
console.log(a); // prints '4'
}
four();
function scope3(print) {
if (print) {
let insideIf = '12';
}
console.log(insideIf);
}
scope3(true); // prints ''
var is20 = false; // boolean
typeof is20; // boolean
var age = 19;
typeof age; // number
var lastName = "Bae";
typeof lastName; // string
var fruits = ["Apple", "Banana", "Kiwi"];
typeof fruits; // object
var me = {firstName:"Sammie", lastName:"Bae"};
typeof me; // object
var nullVar = null;
typeof nullVar; // object
var function1 = function(){
console.log(1);
}
typeof function1 // function
var blank;
typeof blank; // undefined
// Truthy/falsey check
if (node) {
}
var printIfTrue = '';
if (printIfTrue) {
console.log('truthy');
} else {
console.log('falsey'); // prints 'falsey'
}
"5" == 5 // returns true
"5" === 5 // returns false
var o1 = {};
var o2 = {};
o1 == o2 // returns false
o1 === o2 // returns false
var o1 = {};
var o2 = o1;
o1 == o2 // returns true
o1 === o2 // returns true
function isEquivalent(a, b) {
// arrays of property names
var aProps = Object.getOwnPropertyNames(a);
var bProps = Object.getOwnPropertyNames(b);
// If their property lengths are different, they're different objects
if (aProps.length != bProps.length) {
return false;
}
for (var i = 0; i < aProps.length; i++) {
var propName = aProps[i];
// If the values of the property are different, not equal
if (a[propName] !== b[propName]) {
return false;
}
}
// If everything matched, correct
return true;
}
isEquivalent({'hi':12},{'hi':12}); // returns true
var obj1 = {'prop1': 'test','prop2': function (){} };
var obj2 = {'prop1': 'test','prop2': function (){} };
isEquivalent(obj1,obj2); // returns false
var function1 = function(){console.log(2)};
var function2 = function(){console.log(2)};
console.log(function1 == function2); // prints 'false'
void 0 === undefined; // true