@@ -30,7 +30,7 @@ <h1>JavaScript Test: 5</h1>
30
30
==========================================================================================================
31
31
*/
32
32
// Q1. a
33
- const weightQa = ( m , g = 10 ) => m * g ;
33
+ const weightQa = ( m , g = 10 ) => ( m * g ) . toFixed ( 2 ) + ' N' ;
34
34
console . log ( weightQa ( 100 ) ) ; //Earth
35
35
// 1000 N
36
36
console . log ( weightQa ( 100 , 25 ) ) ; //Jupiter
@@ -41,21 +41,31 @@ <h1>JavaScript Test: 5</h1>
41
41
// 162 N
42
42
// Q1. b
43
43
const weightOfObject = ( m , planet ) => {
44
+ let result ;
44
45
switch ( planet . toLowerCase ( ) ) {
45
46
case 'sun' :
46
- return 274 * m + ' N' ;
47
+ result = 274 * m ;
48
+ break ;
47
49
case 'jupiter' :
48
- return 25 * m + ' N' ;
50
+ result = 25 * m ;
51
+ break ;
49
52
case 'earth' :
50
- return ( 9.79 * m ) . toFixed ( 2 ) + ' N' ;
53
+ result = 9.79 * m ;
54
+ break ;
51
55
case 'venus' :
52
- return ( 8.87 * m ) . toFixed ( 2 ) + ' N' ;
56
+ result = 8.87 * m ;
57
+ break ;
53
58
case 'mars' :
54
- return 3.7 * m + ' N' ;
59
+ result = 3.7 * m ;
60
+ break ;
55
61
case 'moon' :
56
- return 1.62 * m + ' N' ;
62
+ result = 1.62 * m ;
63
+ break ;
57
64
default :
58
- }
65
+ result = 'Not a valid input'
66
+ } ;
67
+ if ( typeof result === 'number' ) return result . toFixed ( 2 ) + ' N' ;
68
+ else result ;
59
69
} ;
60
70
61
71
console . log ( weightOfObject ( 100 , 'earth' ) ) ;
@@ -69,7 +79,7 @@ <h1>JavaScript Test: 5</h1>
69
79
==========================================================================================================
70
80
*/
71
81
const sentence = `%I $am@% a %tea@cher%, &and& I lo%#ve %tea@ching%;. There $is nothing; &as& mo@re rewarding as educa@ting &and& @emp%o@wering peo@ple. ;I found tea@ching m%o@re interesting tha@n any other %jo@bs. %Do@es thi%s mo@tivate yo@u to be a tea@cher!?` ;
72
- const cleanText = sent => sent . replace ( / [ , ; # % & @ ! $ ' ] / g, '' ) ;
82
+ const cleanText = ( rawText ) => rawText . replace ( / [ , ; # % & @ ! $ ' ] / g, '' ) ;
73
83
console . log ( cleanText ( sentence ) ) ;
74
84
/*
75
85
=============================== QUESTION 3 ===========================================================
@@ -82,9 +92,7 @@ <h1>JavaScript Test: 5</h1>
82
92
let count = 0 ;
83
93
const words = text . replace ( / [ , ; # % & @ ! $ ' . ? ] / g, '' ) . split ( ' ' ) ;
84
94
for ( const word of words ) {
85
- if ( word . length > 1 ) {
86
- count ++ ;
87
- }
95
+ if ( word . length > 1 ) count ++ ;
88
96
}
89
97
90
98
return count ;
@@ -96,14 +104,8 @@ <h1>JavaScript Test: 5</h1>
96
104
======================================================================================================
97
105
*/
98
106
const countVarietyOfWords = sent => {
99
- let count = 0 ;
100
107
const words = sent . replace ( / [ , ; # % & @ ! $ ' . ? ] / g, '' ) . split ( ' ' ) ;
101
- const newWords = new Set ( ) ;
102
- for ( const word of words ) {
103
- newWords . add ( word ) ;
104
- }
105
- console . log ( Array . from ( newWords ) ) ;
106
-
108
+ const newWords = new Set ( words ) ;
107
109
return newWords . size ;
108
110
} ;
109
111
console . log ( countVarietyOfWords ( sentence ) ) ;
@@ -115,11 +117,13 @@ <h1>JavaScript Test: 5</h1>
115
117
==========================================================================================================
116
118
*/
117
119
const supportWords = [
118
- 'I ' ,
120
+ 'i ' ,
119
121
'me' ,
120
122
'mine' ,
123
+ 'my' ,
121
124
'myself' ,
122
125
'you' ,
126
+ 'your' ,
123
127
'yours' ,
124
128
'yourself' ,
125
129
'he' ,
@@ -211,16 +215,13 @@ <h1>JavaScript Test: 5</h1>
211
215
for ( const word of words ) {
212
216
if ( word . length > 1 && supportWords . indexOf ( word . toLowerCase ( ) ) < 0 ) {
213
217
newWords . push ( word ) ;
218
+ count ++ ;
214
219
}
215
220
}
216
- // console.log(Array.from(newWords));
217
- // console.log(newWords);
218
-
219
- // return newWords.size;
220
- return newWords ;
221
+ return { words : newWords , count}
221
222
} ;
222
223
console . log ( countOnlyMainWords ( sentence ) ) ;
223
- console . log ( countOnlyMainWords ( sentence ) ) ;
224
+
224
225
225
226
/*
226
227
=============================== QUESTION 6 =================================================================
@@ -273,16 +274,14 @@ <h1>JavaScript Test: 5</h1>
273
274
const stat = arr => {
274
275
const mean = ( ) => {
275
276
let sum = 0 ;
276
- let average ;
277
277
for ( item of arr ) {
278
278
sum += item ;
279
279
}
280
- average = sum / arr . length ;
281
- return average ;
280
+
281
+ return Math . round ( sum / arr . length ) ;
282
282
} ;
283
283
const median = ( ) => {
284
284
const arrangedArray = [ ...arr ] . sort ( ( a , b ) => a - b ) ;
285
- console . log ( arrangedArray ) ;
286
285
const index = arrangedArray . length / 2 ;
287
286
288
287
if ( arrangedArray . length % 2 === 0 ) {
@@ -296,7 +295,6 @@ <h1>JavaScript Test: 5</h1>
296
295
} ;
297
296
const range = ( ) => {
298
297
const sortedArray = [ ...arr ] . sort ( ( a , b ) => a - b ) ;
299
- console . log ( sortedArray ) ;
300
298
const lastIndex = sortedArray . length - 1 ;
301
299
return sortedArray [ lastIndex ] - sortedArray [ 0 ] ;
302
300
} ;
@@ -328,7 +326,7 @@ <h1>JavaScript Test: 5</h1>
328
326
} ;
329
327
330
328
return {
331
- mean : mean ( ) . toFixed ( 2 ) ,
329
+ mean : mean ( ) ,
332
330
median : median ( ) ,
333
331
range : range ( ) ,
334
332
mode : mode ( )
@@ -359,10 +357,10 @@ <h1>JavaScript Test: 5</h1>
359
357
77.5 ,
360
358
82.5
361
359
] ;
362
- console . log ( stat ( scores ) . mean ) ;
363
- console . log ( stat ( scores ) . median ) ;
364
- console . log ( stat ( scores ) . range ) ;
365
- console . log ( stat ( scores ) . mode ) ;
360
+ console . log ( 'mean' , stat ( scores ) . mean ) ;
361
+ console . log ( 'median' , stat ( scores ) . median ) ;
362
+ console . log ( 'range' , stat ( scores ) . range ) ;
363
+ console . log ( 'mode' , stat ( scores ) . mode ) ;
366
364
367
365
/*
368
366
=============================== QUESTION 8 =================================================================
@@ -375,11 +373,9 @@ <h1>JavaScript Test: 5</h1>
375
373
// generate array of rgb colors numbers.
376
374
377
375
const arrayOfRgbColors = ( n = 1 ) => {
378
- let red ;
379
- let green ;
380
- let blue ;
376
+ let red , green , blue ;
381
377
let rgb ;
382
- let rgbColors = [ ] ;
378
+ const rgbColors = [ ] ;
383
379
for ( let i = 0 ; i < n ; i ++ ) {
384
380
red = Math . floor ( Math . random ( ) * 256 ) ;
385
381
green = Math . floor ( Math . random ( ) * 256 ) ;
@@ -391,6 +387,9 @@ <h1>JavaScript Test: 5</h1>
391
387
return rgbColors ;
392
388
} ;
393
389
console . log ( arrayOfRgbColors ( ) ) ;
390
+ console . log ( arrayOfRgbColors ( 2 ) ) ;
391
+ console . log ( arrayOfRgbColors ( 5 ) ) ;
392
+
394
393
// generate array of hexa colors numbers.
395
394
const arrayOfHexaColors = ( n = 1 ) => {
396
395
const lettersNumbers = '0123456789abcdef' ;
@@ -409,6 +408,8 @@ <h1>JavaScript Test: 5</h1>
409
408
return hexaColors ;
410
409
} ;
411
410
console . log ( arrayOfHexaColors ( ) ) ;
411
+ console . log ( arrayOfHexaColors ( 2 ) ) ;
412
+ console . log ( arrayOfHexaColors ( 5 ) ) ;
412
413
413
414
/*
414
415
=============================== QUESTION 9 =================================================================
@@ -418,7 +419,7 @@ <h1>JavaScript Test: 5</h1>
418
419
419
420
const arrayOfHexaOrRgb = ( color = 'hexa' , n = 1 ) => {
420
421
let colors ;
421
- let value = color . toLowerCase ( ) ;
422
+ const value = color . toLowerCase ( ) ;
422
423
if ( 'hexa' === value || 'hex' === value || 'hexadecimal' === value ) {
423
424
colors = arrayOfHexaColors ( n ) ;
424
425
} else if ( 'rgb' === value || 'RGB' === value ) {
@@ -452,19 +453,51 @@ <h1>JavaScript Test: 5</h1>
452
453
c. Do you think there is similarity between the speeches ?
453
454
============================================================================================================
454
455
*/
455
-
456
- console . log ( countOnlyMainWords ( melina ) ) ;
457
- console . log ( countOnlyMainWords ( michelle ) ) ;
458
-
459
- const removeRepeatedWords = arr => {
456
+ const removeRepeatedWords = arr => {
460
457
const set = new Set ( arr ) ;
461
458
return Array . from ( set ) ;
462
459
} ;
463
- console . log ( removeRepeatedWords ( countOnlyMainWords ( melina ) ) . sort ( ) ) ;
464
- console . log ( removeRepeatedWords ( countOnlyMainWords ( michelle ) ) . sort ( ) ) ;
465
460
466
- const melinaWords = removeRepeatedWords ( countOnlyMainWords ( melina ) ) ;
467
- const michelleWords = removeRepeatedWords ( countOnlyMainWords ( michelle ) ) ;
461
+ const mostTenRepeatedWords = ( words ) => {
462
+ const map = new Map ( ) ;
463
+ for ( word of words ) {
464
+ if ( ! map . has ( word ) ) {
465
+ map . set ( word , 1 ) ;
466
+ } else {
467
+ map . set ( word , map . get ( word ) + 1 ) ;
468
+ }
469
+ }
470
+
471
+ const sortItems = arr => {
472
+ let sortedArray = [ ...arr ] ;
473
+ sortedArray . sort ( ( a , b ) => {
474
+ if ( a [ 1 ] > b [ 1 ] ) {
475
+ return - 1 ;
476
+ } else if ( a [ 1 ] < b [ 1 ] ) {
477
+ return 1 ;
478
+ } else {
479
+ return 0 ;
480
+ }
481
+ } ) ;
482
+ return sortedArray ;
483
+ } ;
484
+ return sortItems ( Array . from ( map ) ) . slice ( 0 , 10 )
485
+
486
+ }
487
+ console . log ( mostTenRepeatedWords ( countOnlyMainWords ( melina ) . words ) ) // melinas 10 most repeated words
488
+ console . log ( mostTenRepeatedWords ( countOnlyMainWords ( michelle ) . words ) ) // michelle's 10 most repeated words
489
+ // console.log(countOnlyMainWords(melina));
490
+ // console.log(countOnlyMainWords(michelle));
491
+
492
+
493
+ console . log ( removeRepeatedWords ( countOnlyMainWords ( melina ) . words ) . sort ( ) ) ;
494
+ console . log ( removeRepeatedWords ( countOnlyMainWords ( michelle ) . words ) . sort ( ) ) ;
495
+
496
+ const melinaWords = removeRepeatedWords ( countOnlyMainWords ( melina ) . words ) ;
497
+ const michelleWords = removeRepeatedWords ( countOnlyMainWords ( michelle ) . words ) ;
498
+
499
+ // common words
500
+ // check the similarity common words divided by their the total number of non repeated words
468
501
const checkSimilarity = ( firstWords , secondWords ) => {
469
502
const commonWords = [ ] ;
470
503
for ( firstWord of firstWords ) {
@@ -477,8 +510,8 @@ <h1>JavaScript Test: 5</h1>
477
510
return commonWords ;
478
511
} ;
479
512
console . log ( checkSimilarity ( michelleWords , melinaWords ) ) ;
480
- console . log ( melinaWords . length / 16 ) ;
481
- console . log ( michelleWords . length / 16 ) ;
513
+ console . log ( Math . round ( 100 * 160 / melinaWords . length ) ) ; // the similarity between the speech is so high
514
+ console . log ( Math . round ( 100 * 160 / michelleWords . length ) ) ;
482
515
</ script >
483
516
</ body >
484
517
</ html >
0 commit comments