@@ -44,27 +44,27 @@ <h1>JavaScript Test: 5</h1>
44
44
let result ;
45
45
switch ( planet . toLowerCase ( ) ) {
46
46
case 'sun' :
47
- result = 274 * m ;
47
+ result = 274 * m ;
48
48
break ;
49
49
case 'jupiter' :
50
- result = 25 * m ;
50
+ result = 25 * m ;
51
51
break ;
52
52
case 'earth' :
53
- result = 9.79 * m ;
53
+ result = 9.79 * m ;
54
54
break ;
55
55
case 'venus' :
56
- result = 8.87 * m ;
56
+ result = 8.87 * m ;
57
57
break ;
58
58
case 'mars' :
59
- result = 3.7 * m ;
59
+ result = 3.7 * m ;
60
60
break ;
61
61
case 'moon' :
62
- result = 1.62 * m ;
62
+ result = 1.62 * m ;
63
63
break ;
64
64
default :
65
- result = 'Not a valid input'
66
- } ;
67
- if ( typeof result === 'number' ) return result . toFixed ( 2 ) + ' N' ;
65
+ result = 'Not a valid input' ;
66
+ }
67
+ if ( typeof result === 'number' ) return result . toFixed ( 2 ) + ' N' ;
68
68
else result ;
69
69
} ;
70
70
@@ -79,7 +79,7 @@ <h1>JavaScript Test: 5</h1>
79
79
==========================================================================================================
80
80
*/
81
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!?` ;
82
- const cleanText = ( rawText ) => rawText . replace ( / [ , ; # % & @ ! $ ' ] / g, '' ) ;
82
+ const cleanText = rawText => rawText . replace ( / [ , ; # % & @ ! $ ' ] / g, '' ) ;
83
83
console . log ( cleanText ( sentence ) ) ;
84
84
/*
85
85
=============================== QUESTION 3 ===========================================================
@@ -92,7 +92,7 @@ <h1>JavaScript Test: 5</h1>
92
92
let count = 0 ;
93
93
const words = text . replace ( / [ , ; # % & @ ! $ ' . ? ] / g, '' ) . split ( ' ' ) ;
94
94
for ( const word of words ) {
95
- if ( word . length > 1 ) count ++ ;
95
+ if ( word . length > 1 ) count ++ ;
96
96
}
97
97
98
98
return count ;
@@ -218,10 +218,9 @@ <h1>JavaScript Test: 5</h1>
218
218
count ++ ;
219
219
}
220
220
}
221
- return { words : newWords , count}
221
+ return { words : newWords , count } ;
222
222
} ;
223
223
console . log ( countOnlyMainWords ( sentence ) ) ;
224
-
225
224
226
225
/*
227
226
=============================== QUESTION 6 =================================================================
@@ -277,7 +276,7 @@ <h1>JavaScript Test: 5</h1>
277
276
for ( item of arr ) {
278
277
sum += item ;
279
278
}
280
-
279
+
281
280
return Math . round ( sum / arr . length ) ;
282
281
} ;
283
282
const median = ( ) => {
@@ -453,48 +452,50 @@ <h1>JavaScript Test: 5</h1>
453
452
c. Do you think there is similarity between the speeches ?
454
453
============================================================================================================
455
454
*/
456
- const removeRepeatedWords = arr => {
455
+ const removeRepeatedWords = arr => {
457
456
const set = new Set ( arr ) ;
458
457
return Array . from ( set ) ;
459
458
} ;
460
459
461
- const mostTenRepeatedWords = ( words ) => {
460
+ const mostTenRepeatedWords = words => {
462
461
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
- }
462
+ for ( word of words ) {
463
+ if ( ! map . has ( word ) ) {
464
+ map . set ( word , 1 ) ;
465
+ } else {
466
+ map . set ( word , map . get ( word ) + 1 ) ;
469
467
}
468
+ }
470
469
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
470
+ const sortItems = arr => {
471
+ let sortedArray = [ ...arr ] ;
472
+ sortedArray . sort ( ( a , b ) => {
473
+ if ( a [ 1 ] > b [ 1 ] ) {
474
+ return - 1 ;
475
+ } else if ( a [ 1 ] < b [ 1 ] ) {
476
+ return 1 ;
477
+ } else {
478
+ return 0 ;
479
+ }
480
+ } ) ;
481
+ return sortedArray ;
482
+ } ;
483
+ return sortItems ( Array . from ( map ) ) . slice ( 0 , 10 ) ;
484
+ } ;
485
+ console . log ( mostTenRepeatedWords ( countOnlyMainWords ( melina ) . words ) ) ; // melinas 10 most repeated words
486
+ console . log ( mostTenRepeatedWords ( countOnlyMainWords ( michelle ) . words ) ) ; // michelle's 10 most repeated words
489
487
// console.log(countOnlyMainWords(melina));
490
488
// console.log(countOnlyMainWords(michelle));
491
489
492
-
493
490
console . log ( removeRepeatedWords ( countOnlyMainWords ( melina ) . words ) . sort ( ) ) ;
494
- console . log ( removeRepeatedWords ( countOnlyMainWords ( michelle ) . words ) . sort ( ) ) ;
491
+ console . log (
492
+ removeRepeatedWords ( countOnlyMainWords ( michelle ) . words ) . sort ( )
493
+ ) ;
495
494
496
495
const melinaWords = removeRepeatedWords ( countOnlyMainWords ( melina ) . words ) ;
497
- const michelleWords = removeRepeatedWords ( countOnlyMainWords ( michelle ) . words ) ;
496
+ const michelleWords = removeRepeatedWords (
497
+ countOnlyMainWords ( michelle ) . words
498
+ ) ;
498
499
499
500
// common words
500
501
// check the similarity common words divided by their the total number of non repeated words
@@ -511,8 +512,8 @@ <h1>JavaScript Test: 5</h1>
511
512
} ;
512
513
console . log ( checkSimilarity ( michelleWords , melinaWords ) ) ;
513
514
const commonWords = checkSimilarity ( michelleWords , melinaWords ) . length ;
514
- console . log ( Math . round ( 100 * commonWords / melinaWords . length ) ) ; // the similarity between the speech is so high
515
- console . log ( Math . round ( 100 * commonWords / michelleWords . length ) ) ;
515
+ console . log ( Math . round ( ( 100 * commonWords ) / melinaWords . length ) ) ; // the similarity between the speech is so high
516
+ console . log ( Math . round ( ( 100 * commonWords ) / michelleWords . length ) ) ;
516
517
</ script >
517
518
</ body >
518
519
</ html >
0 commit comments