Repository navigation
Expand file tree
/
Copy pathAskAnswerJS.html
More file actions
336 lines (285 loc) · 10.6 KB
/
Copy pathAskAnswerJS.html
File metadata and controls
336 lines (285 loc) · 10.6 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul class="list">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<ul class="list2">
<li>Text</li>
<li>Text</li>
<li>Text</li>
<li>Text</li>
<li>Text</li>
</ul>
<form action="" name="myForm" >
<label>
<input name="username" placeholder="Name" type="text" >
</label>
<label>
<input name="email" placeholder="Email" type="text" >
</label>
<button onclick="validateForm()">Submit</button>
</form>
<script>
/***********************************************\
* Skilvul/ImpactByte JavaScript challenge
* Browser support is irrelevant here, but any comments on specific
* methods you use is a plus to show deeper understanding of the language
\***********************************************/
/**
* Task 1: Write a function that repeats the String
* with the following output:
* 'Skilvul'.repeatString(3); // 'SkilvulSkilvulSkilvul';
*/
/*
Answer Task 1:
Create repeatString extension methods on String class.
explanation:
Each object in the JavaScript has a prototype property which can be used for purpose of extent methods. It gives the possibility to extend the object. We can use the String.prototype property and write a method to implements the functionality.
*/
String.prototype.repeatString = function(n) {
return this.repeat(n);
};
console.log('Skilvul'.repeatString(3));
//Test
//should succes when call repeatString
console.assert('Skilvul'.repeatString(3) === 'SkilvulSkilvulSkilvul', 'The repeatString function did not evaluate to the correct repeating string')
/**
* Task 2: Could you find a way to optimize the following code?
*/
/* var list = document.querySelectorAll('.list li');
function logContent() {
console.log(this.innerHTML);
}
for (var i = 0; i < list.length; i++) {
list[i].addEventListener('click', logContent, false);
}
*/
/*
Answer Task 2:
for simpler code change the handler for each element with the adding it to
the parent element
*/
function logContent() {
console.log(this.innerHTML);
}
var listContainer = document.querySelector('.list');
listContainer.addEventListener('click', function(e) {
let targetElement = e.target;
// if the target is a child LI then call the logContent function
if(targetElement.tagName === 'LI') {
logContent.apply(targetElement);
}
}, false);
/**
* Task 3: Inspect the output in the console from clicking an
* item from ".list2", each index is the same
* Explain why, and also fix the problem to log the correct index
*/
/* var list2 = document.querySelectorAll('.list2 li');
for (var i = 0; i < list2.length; i++) {
list2[i].addEventListener('click', function () {
console.log('My index:', i);
}, false);
} */
/*
Answer Task 3:
Basic Concept of Closure
The logged index is always the last (5) because:
1. the handlers are logging the current value of i
2. i is declared in a parent scope
3. when the loop ends, the value of i is 5, so each handler logs 5.
To fix that is to add data-index property, and log the data-index in the event handler
another way is change keyword var with let ,the let keyword make the variable value in
looping be local variable
*/
//solving with data-index
var list2 = document.querySelectorAll('.list2 li');
for (var i = 0; i < list2.length; i++) {
list2[i].dataset.index = i;
list2[i].addEventListener('click', function (evt) {
console.log('My index:', this.dataset.index);
}, false);
}
//solving with change keyword (let) for datatype
var list2 = document.querySelectorAll('.list2 li');
for (let i = 0; i < list2.length; i++) {
list2[i].addEventListener('click', function () {
console.log('My index with let:', i);
}, false);
}
/**
* Task 4: Explain why the logs happen in the following order:
* one, three, two
*/
(function () {
console.log('one');
setTimeout(function() {
console.log('two');
}, 0);
console.log('three');
})();
/**
*Answer 4
* the setTimeout methods is one of the Asynchronous Method in javascript
* so the callback provided in setTimeout function is queued, even if the delay value specified is zero.
* because of that the console.log('two') will be invoked in the next event loop.
* result of that the console will log one, three and two. .
*/
/**
* Task 5: Explain how "this" works in this particular scenario
* (how iPad is logged, followed by iPhone)
*/
var product = 'iPhone';
var obj = {
product: 'iMac',
prop: {
product: 'iPad',
getProductName: function() {
return this.product;
}
}
};
console.log(obj.prop.getProductName());
var test = obj.prop.getProductName;
console.log(test());
/**
*Answer 5:
* Explanation of scope in every context
* have to take into account who is "this" in each Scope.
* for the obj.prop.getProductName() call,
* this === obj.prop
* so this.product === obj.prop.product === 'iPad'
*
* saving the obj.prop.getProductName in the test variable
* and executing it, you get:
* this === window,
* so this.product === window.product === product === 'iPhone'
*
* so how to log iMac?
* var test = obj.prop.getProductName.bind(obj);
* console.log(test());
* in this case:
* this ==== obj
* this.product === obj.product === 'iMac'
*/
/**
* Task 6: Return the file extension or false if no extension. Please
* provide a list of unit-test cases that you would implement in a real
* environment (e.g. it('should do that when this', ...)). Minimal "it"
* function implementation is a plus.
*/
//Answer Task 6
function getFileExtension(file) {
try {
let dotPosition = file.lastIndexOf('.');
// file has no extension
if(dotPosition < 0) {
return false;
}
// assuming that file extension should not contain the dot.
let extension = file.slice(dotPosition + 1);
// can be emtpy extension also, like 'mozio.'
return extension? extension : false;
}
catch (e) {
return false;
}
}
console.log(getFileExtension('skilvul.png'));
// Unit test scenario
console.assert(getFileExtension('skilvul.png') === 'png', 'Should Success to return extension file when the file has extension file');
console.assert(getFileExtension('skilvul.') === false, 'Should fail when the file has no extension');
console.assert(getFileExtension('skilvul.tar.gz') === 'gz', 'Should Success when the file has more dot and has extension');
console.assert(getFileExtension('skilvul') === false, 'Should fail when getFileExtension get string value without dot as parameter');
console.assert(getFileExtension('') === false, 'Should fail when the getFileExtension function get empty string as parameter');
console.assert(getFileExtension() === false, 'Should fail when the getFileExtension did not get parameter');
/**
* Task 7: Return the longest String in the Array and
* in all nested arrays
* function longestString(i) {}
*/
//Answer Task 7
function longestString(array) {
// return the longest string in the array
var longest = 0;
var longestString = "";
for (var i = 0; i < array.length; i++) {
if (Object.prototype.toString.call(array[i]) === '[object Array]') {
array[i].forEach(function (it) {
if (it.length > longest) {
longest = it.length;
longestString = it;
}
})
} else if (Object.prototype.toString.call(array[i]) === '[object Object]') {
for (var j in array[i]) {
if (array[i][j].length > longest) {
longest = array[i][j].length;
longestString = array[i][j];
}
}
} else if (typeof array[i] == "string" && array[i].length > longest) {
longest = array[i].length;
longestString = array[i];
}
}
return longestString;
}
var longest = longestString([
'coca-cola',
'pepsi',
'lemonade',
'red bull',
['im the longest string', ['vodka']]
]);
console.log(longest);
// Unit test
console.assert(longest === 'im the longest string', 'Should success longestString function return the longest string of element array event if in nested array');
/**
* Task 8: Your task is to write a simple form validation script.
* It should be easy to attach the validation script to any "<form>"
* tag with any possible set of fields inside.
* The script should check that:
* - all fields marked with "required" attribute filled out
* - all fields with type "email" filled with a valid email address
* (to validate the email please use the function below)
* - DO NOT rely on HTML5 validation
*
* Please attach your validation script to the example form from the HTML section
* and demonstrate that it correctly validates it.
* PLease note that the validation script should support any "<form>" with any
* set of possible fields inside, the form above is just an example. Feel free
* to change the form to demonstrate that your validation script is universal.
*/
//Answer Task 8
//Validate Email using Regex
function validateEmail(str) {
var regexp = /[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@[a-z0-9-]+(\.[a-z0-9-]+)*/;
return regexp.test(str);
}
function validateForm(){
let username = document.forms["myForm"]["username"].value;
if (username == "") {
alert("username must filled out");
return false;
}
let email = document.forms["myForm"]["email"].value;
// To validate the email,call the validateEmail function
if(!validateEmail(email)){
alert('Email not valid')
return false;
}
return true;
}
</script>
</body>
</html>