-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathjavascript-test-2020.js
780 lines (672 loc) · 19.9 KB
/
javascript-test-2020.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
console.log('==================== BEGIN Q1 ============================ ')
/* === Question 1 === */
// 1. 1
const printHashes = () => {
let hash = ''
for (let i = 0; i < 7; i++) {
hash += '#'
console.log(hash)
}
}
printHashes()
console.log('----------------------------------------------------------- ')
// 1. 2
const multiplicationTable = n => {
for (let i = 0; i < n; i++) {
console.log(`${i} * ${i} = ${i * i}`)
}
}
multiplicationTable(11)
console.log('----------------------------------------------------------- ')
// 1. 3
const exponentialTable = () => {
console.log(`i\ti^2\t^3`)
for (let i = 0; i < 11; i++) {
console.log(`${i}\t${i ** 2}\t${i ** 3}`)
}
}
exponentialTable()
console.log('----------------------------------------------------------- ')
// 1. 4
const countries = [
'ALBANIA',
'BOLIVIA',
'CANADA',
'DENMARK',
'ETHIOPIA',
'FINLAND',
'GERMANY',
'HUNGARY',
'IRELAND',
'JAPAN',
'KENYA'
]
// const createArrayOfArrays = arr => {
// const newArray = []
// for (const element of arr) {
// let name = element[0] + element.slice(1).toLowerCase()
// newArray.push([name, element.slice(0, 3), element.length])
// }
// return newArray
// }
const createArrayOfArrays = arr =>
arr.map(country => {
let name = country[0] + country.slice(1).toLowerCase()
return [name, country.slice(0, 3), country.length]
})
console.log(createArrayOfArrays(countries))
console.log('==================== END Q1 ============================ ')
console.log('==================== BEGIN Q2 ============================ ')
/* === Question 2 === */
// 2. 1
const products = [
{ product: 'banana', price: 3 },
{ product: 'mango', price: 6 },
{ product: 'potato', price: ' ' },
{ product: 'avocado', price: 8 },
{ product: 'coffee', price: 10 },
{ product: 'tea', price: '' }
]
const printProductItems = arr => {
for (const { product, price } of arr) {
let formattedPrice = typeof price == 'number' ? price : 'unknown'
console.log(`The price of ${product} is ${formattedPrice}`)
}
}
printProductItems(products)
console.log('----------------------------------------------------------- ')
// 2. 2
const sumOfAllPrices = arr => {
let total = 0
for (const { price } of arr) {
if (typeof price == 'number') {
total += price
}
}
return total
}
console.log('the sum of all prices using for of', sumOfAllPrices(products))
console.log('----------------------------------------------------------- ')
// 2. 3
const total = products
.map(prod => prod.price)
.filter(price => typeof price == 'number')
.reduce((curr, acc) => curr + acc)
console.log('total from method chaining', total)
console.log('----------------------------------------------------------- ')
// 2. 4
const totalUsingReduce = products.reduce((accu, curr) => {
let price = typeof curr.price == 'number' ? curr.price : 0
return accu + price
}, 0)
console.log('reduce total', totalUsingReduce)
console.log(sumOfAllPrices(products))
console.log('==================== END Q2 ============================ ')
console.log('==================== BEGIN Q3 ============================ ')
/*=== Question 3 === */
// 3. 1
const howManyDaysInMonth = () => {
const userInput = prompt('Enter a month: ')
.trim()
.toLowerCase()
const firstLetter = userInput[0].toUpperCase()
const remainingStr = userInput.slice(1)
const month = firstLetter + remainingStr
let statement
let days
switch (userInput) {
case 'february':
days = 28
statement = `${month} has ${days} days.`
break
case 'april':
case 'june':
case 'september':
case 'november':
days = 30
statement = `${month} has ${days} days.`
break
case 'january':
case 'march':
case 'may':
case 'july':
case 'august':
case 'october':
case 'december':
days = 31
statement = `${month} has ${days} days.`
break
default:
return 'The given value is not a month'
}
return statement
}
console.log(howManyDaysInMonth())
console.log('----------------------------------------------------------- ')
// 3. 2
const generate = (type = 'id') => {
const randomId = (n = 6) => {
const str = '0123456ABCDEFGHIJKLMNOPKRSTUVWXYZabcdefghihjklmnopqrstuvwxyz'
let id = ''
for (let i = 0; i < n; i++) {
let index = Math.floor(Math.random() * str.length)
id = id + str[index]
}
return id
}
const hexaColor = function() {
let st = '0123456789abcdef'.split('')
let color = '#'
for (let i = 0; i < 6; i++) {
let index = Math.floor(Math.random() * st.length)
color += st[index]
}
return color
}
const rgbColor = function() {
let redColor = Math.floor(Math.random() * 256)
let greenColor = Math.floor(Math.random() * 256)
let blueColor = Math.floor(Math.random() * 256)
const rgb = `rgb(${redColor},${greenColor},${blueColor})`
return rgb
}
switch (type.toLowerCase()) {
case 'id':
return randomId()
case 'hexa':
case 'hexadecimal':
return hexaColor()
case 'rgb':
return rgbColor()
default:
return 'Not a valid format'
}
}
console.log(generate())
console.log(generate('id'))
console.log(generate('hexa'))
console.log(generate('hexadecimal'))
console.log(generate('rgb'))
console.log(generate('RGB'))
console.log(generate('rgb'))
console.log('==================== END Q3 ============================ ')
console.log('==================== BEGIN Q4 ============================ ')
/*=== Question 4 ==== */
// 4. 1
const countries2 = [
'ALBANIA',
'BOLIVIA',
'CANADA',
'DENMARK',
'ETHIOPIA',
'FINLAND',
'GERMANY',
'HUNGARY',
'IRELAND',
'JAPAN',
'KENYA'
]
const c = ['ESTONIA', 'FRANCE', 'GHANA']
countries2.splice(4, 3, ...c)
console.log(countries2)
console.log('----------------------------------------------------------- ')
// 4. 2
const checkUniqueness = arr => {
for (const element of arr) {
if (arr.indexOf(element) !== arr.lastIndexOf(element)) {
return false
}
}
return true
}
const arrOne = [1, 4, 6, 2, 1]
console.log(checkUniqueness(arrOne)) // false
const arrTwo = [1, 4, 6, 2, 3]
console.log(checkUniqueness(arrTwo)) // true
console.log('----------------------------------------------------------- ')
// 4. 3
const checkDataTypes = (arr, type) => arr.every(elem => typeof elem === type)
const numbers = [1, 3, 4]
const names = ['Asab', '30DaysOfJavaScript']
const bools = [true, false, true, true, false]
const mixedData = ['Asab', 'JS', true, 2019, { name: 'Asab', lang: 'JS' }]
const obj = [{ name: 'Asab', lang: 'JS' }]
console.log(checkDataTypes(numbers, 'number')) // true
console.log(checkDataTypes(numbers, 'string')) // false
console.log(checkDataTypes(names, 'string')) // true
console.log(checkDataTypes(bools, 'boolean')) // true
console.log(checkDataTypes(mixedData, 'boolean')) // false
console.log(checkDataTypes(obj, 'object')) // true
console.log('==================== END Q4 ============================ ')
console.log('==================== BEGIN Q5 ============================ ')
/*=== Question 5 ==== */
// 5. 1
const fullStack = [
['HTML', 'CSS', 'JS', 'React'],
['Node', 'Express', 'MongoDB']
]
const [frontEnd, backEnd] = fullStack
console.log(frontEnd, backEnd)
console.log('----------------------------------------------------------- ')
// 5. 2
const student = ['David', ['HTM', 'CSS', 'JS', 'React'], [98, 85, 90, 95]]
const [
name,
[html, css, js, react],
[htmlScore, cssScore, jsScore, reactScore]
] = student
console.log(
name,
html,
css,
js,
react,
htmlScore,
cssScore,
jsScore,
reactScore
)
console.log('----------------------------------------------------------- ')
// 5. 3
const students = [
['David', ['HTM', 'CSS', 'JS', 'React'], [98, 85, 90, 95]],
['John', ['HTM', 'CSS', 'JS', 'React'], [85, 80, 85, 80]]
]
// one way
// const convertArrayToObject = arr => {
// const newArray = []
// for (const [name, skills, scores] of arr) {
// newArray.push({ name, skills, scores })
// }
// return newArray
// }
// another way
const convertArrayToObject = arr =>
arr.map(([name, skills, scores]) => {
return { name, skills, scores }
})
console.log(convertArrayToObject(students))
console.log('==================== END Q5 ============================ ')
console.log('==================== BEGIN Q6 ============================ ')
/*=== Question 6 ==== */
// 6. 1
const sumOfAllNums = (...args) => {
let sum = 0
for (const element of args) {
sum += element
}
return sum
}
console.log(sumOfAllNums(2, 3, 1)) // 6
console.log(sumOfAllNums(1, 2, 3, 4, 5)) // 15
console.log(sumOfAllNums(1000, 900, 120)) // 2020
function sumOfAllNums2() {
let sum = 0
for (let i = 0; i < arguments.length; i++) {
sum += arguments[i]
}
return sum
}
console.log(sumOfAllNums2(2, 3, 1)) // 6
console.log(sumOfAllNums2(1, 2, 3, 4, 5)) // 15
console.log(sumOfAllNums2(1000, 900, 120)) // 2020
console.log('----------------------------------------------------------- ')
// 6. 2
const [x, y] = [2, a => a ** 2 - 4 * a + 3]
const valueOfX = x // 2
const valueOfY = y // (a) => a ** 2 - 4 * a + 3
console.log(valueOfX, valueOfY)
console.log(y(x)) // -1
console.log('----------------------------------------------------------- ')
// 6. 3
const studentObj = {
name: 'David',
age: 25,
skills: {
frontEnd: [
{ skill: 'HTML', level: 10 },
{ skill: 'CSS', level: 8 },
{ skill: 'JS', level: 8 },
{ skill: 'React', level: 9 }
],
backEnd: [
{ skill: 'Node', level: 7 },
{ skill: 'GraphQL', level: 8 }
],
dataBase: [{ skill: 'MongoDB', level: 7.5 }],
dataScience: ['Python', 'R', 'D3.js']
}
}
const devOps = [
{ skill: 'AWS', level: 7 },
{ skill: 'Jenkin', level: 7 },
{ skill: 'Git', level: 8 }
]
const copiedStudentObj = {
...studentObj,
skills: {
...studentObj.skills,
frontEnd: [...studentObj.skills.frontEnd, { skill: 'Bootstrap', level: 8 }],
backEnd: [...studentObj.skills.backEnd, { skill: 'Express', level: 8 }],
dataBase: [...studentObj.skills.dataBase, { skill: 'SQL', level: 8 }],
dataScience: [...studentObj.skills.dataScience, 'SQL'],
devOps: [...devOps]
}
}
console.log(copiedStudentObj)
console.log('==================== END Q6 ============================ ')
console.log('==================== BEGIN Q7 ============================ ')
/*=== Question 7 ==== */
// 7. 1
const showDateTime = (format = 'dd/mm/yyyy') => {
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]
const now = new Date()
const year = now.getFullYear()
const month = months[now.getMonth()]
const mm = now.getMonth() + 1
const date = now.getDate()
const dd = now.getDate()
let hours = now.getHours()
let hh = now.getHours()
let minutes = now.getMinutes()
let seconds = now.getSeconds()
if (hours < 10) {
hours = '0' + hours
}
if (minutes < 10) {
minutes = '0' + minutes
}
if (seconds < 10) {
seconds = '0' + seconds
}
const dateMonthYear = `${month} ${date}, ${year}`
const time = hours + ':' + minutes
const fullTime = dateMonthYear + ' ' + time
// return fullTime + `:${seconds}`
switch (format) {
case 'dd/mm/yyyy':
return `${dd}/${mm}/${year}`
case 'dd-mm-yyyy':
return `${dd}-${mm}-${year}`
case 'dd/mm/yyyy hh:mm':
return `${dd}/${mm}/${year} ${hours}:${minutes}`
case 'dd-mm-yyyy hh:mm':
return `${dd}-${mm}-${year} ${hours}:${minutes}`
case 'MMM DD, YYYY':
return `${month} ${dd}, ${year}`
case 'MMM DD, YYYY hh:mm':
return `${month} ${dd}, ${year} ${hours}:${minutes}`
default:
return `${dd}/${mm}/${year}`
}
}
console.log(showDateTime())
console.log(showDateTime('dd-mm-yyyy'))
console.log(showDateTime('dd-mm-yyyy hh:mm'))
console.log(showDateTime('dd/mm/yyyy hh:mm'))
console.log(showDateTime('Month DD, YYYY'))
console.log(showDateTime('MMM DD, YYYY hh:mm'))
console.log('----------------------------------------------------------- ')
// 7. 1
const todoList = [
{
task: 'Prepare JS Test',
time: '5/4/2020 8:30',
completed: true
},
{
task: 'Give JS Test',
time: '6/4/2020 10:00',
completed: false
},
{
task: 'Assess Test Result',
time: '4/3/2019 1:00',
completed: false
}
]
const addTask = task => {
const time = showDateTime('dd-mm-yyyy hh:mm')
const completed = false
todoList.push({ task, time, completed })
}
const removeTask = index => {
todoList.splice(index, 1)
}
const editTask = (index, newTask) => {
todoList[index].task = newTask
todoList.push({ task, time, completed })
}
const toggleTask = index => {
todoList[index].completed = !todoList[index].completed
}
const toggleAll = () => {
const checkCompleted = todoList.filter(todo => todo.completed === true).length
if (checkCompleted.length === todoList.length) {
for (const item of todoList) {
item.completed = !item.completed
}
} else {
for (const item of todoList) {
item.completed = true
}
}
}
console.log(todoList)
toggleAll()
console.log(todoList)
const removeAll = () => {
// todoList = []
todoList.splice()
}
console.log('==================== END Q7 ============================ ')
console.log('==================== BEGIN Q8 ============================ ')
/*=== Question 8 ==== */
// 8. 1
// generic function to sort items both for string and number
const sortItems = (arr, key) => {
const copiedArr = [...arr]
copiedArr.sort((a, b) => {
if (a[key] > b[key]) return -1
if (a[key] < b[key]) return 1
else return 0
})
return copiedArr
}
const largestCountries = async (n = 10) => {
const API_URL = 'https://restcountries.eu/rest/v2/all'
const countriesArea = []
const response = await fetch(API_URL)
const data = await response.json()
for (const { name, area } of data) {
countriesArea.push({ country: name, area })
}
const countriesSortedByArea = sortItems(countriesArea, 'area').slice(0, n)
console.log(`${n} most largest countries`, countriesSortedByArea)
}
largestCountries(10)
const numberOfLanguages = async () => {
const API_URL = 'https://restcountries.eu/rest/v2/all'
const langSet = new Set()
const response = await fetch(API_URL)
const data = await response.json()
for (const { languages } of data) {
for (const { name } of languages) {
langSet.add(name)
}
}
console.log(Array.from(langSet).sort())
console.log(
'Total number of langauges in the countries API:',
Array.from(langSet).length
)
console.log('----------------------------------------------------------- ')
}
numberOfLanguages()
const mostSpokenLanguages = async (n = 10) => {
const API_URL = 'https://restcountries.eu/rest/v2/all'
const langSet = new Set()
const allLangArr = []
const languageFrequency = []
try {
const response = await fetch(API_URL)
const data = await response.json()
for (const { languages } of data) {
for (const { name } of languages) {
allLangArr.push(name)
langSet.add(name)
}
}
for (l of langSet) {
const x = allLangArr.filter(ln => l == ln)
languageFrequency.push({
lang: l,
count: x.length
})
}
const sortedLanguages = sortItems(languageFrequency, 'count').slice(0, n)
console.log(`${n} most spoken languages`, sortedLanguages)
} catch {
console.log('Something goes wrong')
}
console.log('----------------------------------------------------------- ')
}
console.log('Most spoken languages', mostSpokenLanguages(15))
// 8.2
const add = (a, b) => {
if (b) {
return a + b
}
if (!a) {
return 'at least one parameter is required'
}
return b => a + b
}
console.log(add(2, 3))
console.log(add())
console.log(add(2)(3))
console.log('==================== END Q8 ============================ ')
console.log('==================== BEGIN Q9 ============================ ')
/*=== Question 9 ==== */
/*
9.1 What is the difference between forEach, map, filter and reduce? (1 pt)
9.2 What is the difference between find and filter? (1pt)
9.3 Which of the following mutate array: map, filter, reduce, slice, splice, concat, sort, some? (2pt) only splice and sort modify an array
*/
const generateColor = (type = 'hexa', n = 1) => {
const hexaColor = function() {
let st = '0123456789abcdef'.split('')
let color = '#'
for (let i = 0; i < 6; i++) {
let index = Math.floor(Math.random() * st.length)
color += st[index]
}
return color
}
const rgbColor = function() {
let redColor
let greenColor
let blueColor
redColor = Math.floor(Math.random() * 256)
greenColor = Math.floor(Math.random() * 256)
blueColor = Math.floor(Math.random() * 256)
const rgb = `rgb(${redColor},${greenColor},${blueColor})`
return rgb
}
const rgbColors = []
const hexaColors = []
if (n > 1) {
if (type == 'hexa') {
for (let i = 0; i < n; i++) {
hexaColors.push(hexaColor())
}
} else if (type == 'rgb') {
for (let i = 0; i < n; i++) {
rgbColors.push(rgbColor())
}
}
}
switch (type) {
case 'hexa':
const hexa = n > 1 ? hexaColors : hexaColor()
return hexa
case 'rgb':
const rgb = n > 1 ? rgbColors : rgbColor()
return rgb
default:
return hexaColor()
}
}
console.log(generateColor())
console.log(generateColor('hexa'))
console.log(generateColor('rgb'))
console.log(generateColor('hexa', 3))
console.log(generateColor('rgb', 3))
console.log('==================== END Q9 ============================ ')
console.log('==================== BEGIN Q10 ============================ ')
/*=== Question 10 ==== */
const para = `Studies that estimate and rank the most common words in English examine texts written in English. Perhaps the most comprehensive such analysis is one that was conducted against the Oxford English Corpus (OEC), a very large collection of texts from around the world that are written in the English language. A text corpus is a large collection of written works that are organised in a way that makes such analysis easier.`
const cleanText = txt => {
const pattern = /[^\w ]/g
return txt.replace(pattern, '')
}
const mostFrequentWord = (txt, n = 10) => {
const cleanedText = cleanText(txt)
const words = cleanedText.split(' ')
const map = new Map()
for (const word of words) {
if (map.has(word)) {
let count = map.get(word.toLowerCase()) + 1
map.set(word.toLowerCase(), count)
} else {
map.set(word.toLowerCase(), 1)
}
}
return Array.from(map)
.map(([word, count]) => {
return { word, count }
})
.slice(0, n)
}
console.log('Most frequent words', sortItems(mostFrequentWord(para), 'count'))
console.log('----------------------------------------------------------- ')
// 10. 2
const sentence = `@He@ @%met%^$##%# his mom at no@on and s@he was watching %^$#an epso@ide%^$# of the begni@nging of her Sol@os. Her te@net%^$# hel@ped %^$#her to le@vel up her stats. %^$#After that h@e went to %^$#kayak driving Civic %^$#Honda.`
const cleanedText = cleanText(sentence)
console.log(cleanedText)
console.log('----------------------------------------------------------- ')
// 10. 3
const checkPalindrome = param => {
const word = typeof param == 'number' ? param.toString() : param
let invertedWord = ''
for (let i = word.length - 1; i >= 0; i--) {
invertedWord += word[i]
}
return word.toLowerCase() === invertedWord.toLowerCase()
}
console.log('Check palindrome the number 123:', checkPalindrome(323))
console.log('Check palindrome the word anna:', checkPalindrome('anna'))
console.log('Check palindrome the word He:', checkPalindrome('He'))
console.log('----------------------------------------------------------- ')
// 10.4
const words = cleanText(sentence).split(' ')
const palindromes = words.filter(word => checkPalindrome(word))
const numberOfPalindromes = palindromes.length
console.log(
'Total number of palindrome words in the text:',
numberOfPalindromes
)
console.log('==================== END Q10 ============================ ')