-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.structuredProgramming.js
More file actions
204 lines (167 loc) · 7.7 KB
/
Copy path6.structuredProgramming.js
File metadata and controls
204 lines (167 loc) · 7.7 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const customer1 = {
firstName: 'John',
lastName: 'Doe',
email: 'customer1@customer.com',
birthDate: new Date('9/15/1990'), // september 15th
fullName: function () {
return this.firstName + ' ' + this.lastName
},
age: function () {
const ageDifMs = Date.now() - this.birthDate.getTime()
const ageDate = new Date(ageDifMs) // miliseconds from epoch
return Math.abs(ageDate.getUTCFullYear() - 1970)
}
}
const age = customer1.age()
console.log(`${customer1.fullName()} is ${age} years old`)
if (age > 18) {
console.log('Adult')
} else if (age > 13) {
console.log('Teenager. Not a valid customer')
} else {
console.log('Child. Not a valid customer')
}
function isCustomerValidV1 (customer) {
const age = customer.age()
if (age > 18) {
return true
} else {
return false
}
}
console.log(isCustomerValidV1(customer1))
function isCustomerValidV2 (customer) {
return customer.age() > 18
}
console.log(isCustomerValidV2(customer1))
// console.log(isCustomerValidV2()) // Error: No argument
// This is a conditional ternary operator
// Here it is used for argument checking
function isCustomerValidV3 (customer) {
return customer ? customer.age() > 18 : false
}
console.log(isCustomerValidV3(customer1)) // Prints true
console.log(isCustomerValidV3()) // Prints false
const customer2 = {
firstName: 'Juan',
lastName: 'García',
email: 'customer2',
birthDate: new Date('9/15/1990'), // september 15th
fullName: function () {
return this.firstName + ' ' + this.lastName
},
age: function () {
const ageDifMs = Date.now() - this.birthDate.getTime()
const ageDate = new Date(ageDifMs) // miliseconds from epoch
return Math.abs(ageDate.getUTCFullYear() - 1970)
}
}
function isCustomerEmailValidV1 (customer) {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return re.test(customer.email.toLowerCase())
}
console.log(`${customer1.fullName()} email is ${customer1.email} and is valid = ${isCustomerEmailValidV1(customer1)}`)
console.log(`${customer2.fullName()} email is ${customer2.email} and is valid = ${isCustomerEmailValidV1(customer2)}`)
// console.log(`${customer1.fullName()} email is ${customer1.email} and is valid = ${isCustomerEmailValidV1()}`) //undefined
function isCustomerEmailValidV2 (customer) {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return customer ? re.test(customer.email.toLowerCase()) : false
}
console.log(`${customer2.fullName()} email is ${customer2.email} and is valid = ${isCustomerEmailValidV2()}`) // checks undefined
const customer3 = {
firstName: 'Alan',
lastName: 'Turing',
id: 5567,
// email: 'customer2', // email not define
birthDate: new Date('6/23/1912'), // september 15th
fullName: function () {
return this.firstName + ' ' + this.lastName
},
age: function () {
const ageDifMs = Date.now() - this.birthDate.getTime()
const ageDate = new Date(ageDifMs) // miliseconds from epoch
return Math.abs(ageDate.getUTCFullYear() - 1970)
}
}
//console.log(`${customer3.fullName()} email is ${customer3.email} and is valid = ${isCustomerEmailValidV2(customer3)}`) // email is undefined -> error
function isCustomerEmailValidV3 (customer) {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return customer?.email ? re.test(customer.email.toLowerCase()) : false // operator '?' to check undefined. Notice that is diferent to ternary operator
}
console.log(`${customer3.fullName()} email is ${customer3.email} and is valid = ${isCustomerEmailValidV3(undefined)}`)
// Other ways of declaring functions
// assign a variable to a function
const isCustomerEmailValidv4 = function (customer) {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return customer?.email ? re.test(customer.email.toLowerCase()) : false // operator '?' to check undefined. Notice that is diferent to ternary operator
}
console.log('Using variable: ' + isCustomerEmailValidv4(customer3))
// arrow functions
const isCustomerEmailValidv5 = (customer) => {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return customer?.email ? re.test(customer.email.toLowerCase()) : false // operator '?' to check undefined. Notice that is diferent to ternary operator
}
console.log('Using arrow function: ' + isCustomerEmailValidv5(customer3))
const isCustomerEmailValidv6 = customer => { // if there is only one parameter, parenthesis can be omitted
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return customer?.email ? re.test(customer.email.toLowerCase()) : false // operator '?' to check undefined. Notice that is diferent to ternary operator
}
console.log('Using arrow function no parenthesis: ' + isCustomerEmailValidv6(customer3))
const fullNamev2 = customer => customer.firstName + ' ' + customer.lastName // if there is only a return statement this syntax is valid.
console.log('Using function that only includes a return statement: ' + fullNamev2(customer3))
const customer4 = {
firstName: 'Alan',
lastName: 'Turing',
id: 5567,
fullName: function () {
return this.firstName + ' ' + this.lastName
},
}
function isCustomerIdValidV1 (customer) {
const re = /^([0-9]{4})$/
// operator '?' to check undefined. Notice that is diferent to ternary operator
return customer?.id ? re.test(customer.id) : false
}
console.log(`${customer4.fullName()} id is ${customer4.id} and'
+' is valid = ${isCustomerIdValidV1(customer4)}`)
console.log(`${customer4.fullName()} id is ${customer4.id} and'
+ ' is valid = ${isCustomerIdValidV1()}`)
// Other ways of declaring functions
// Assign a variable to a function
const isCustomerIsValidV2 = function (customer) {
const re = /^([0-9]{4})$/
return customer?.id ? re.test(customer.id) : false
}
console.log('Using variable: ' + isCustomerIsValidV2(customer4))
// Using arrow functions
const isCustomerIdValidV3 = (customer) => {
const re = /^([0-9]{4})$/
return customer?.id ? re.test(customer.id) : false
}
console.log('Using arrow function: ' + isCustomerIdValidV3(customer4))
// Using arrow functions without parenthesis
// if there is only one parameter
const isCustomerIdValidV4 = customer => {
const re = /^([0-9]{4})$/
return customer?.id ? re.test(customer.id) : false
}
console.log('Using arrow function, no parenthesis: ' + isCustomerIdValidV4(customer4))
// if there is only a return statement this syntax is valid:
const fullNameV2 = customer => customer.firstName + ' ' + customer.lastName
console.log('Using function that only includes a return statement: '
+ fullNameV2(customer4))
// Loops and iterations
/*const customer1 = { }
const customer2 = { }
const customer3 = { }
const customers = [customer1, customer2, customer3]
// Classic
for (let index = 0, len = customers.length; index < len; ++index) {
console.log(customers[index].fullName())
}
// Extended
for (const customer of customers) {
console.log(customer.fullName())
}
// Functional
customers.forEach(customer => console.log(customer.fullName()))*/