Skip to content

Commit 59ce339

Browse files
committed
solutions edited
1 parent 65313b6 commit 59ce339

File tree

1 file changed

+85
-52
lines changed

1 file changed

+85
-52
lines changed

solutions/solutons-test-5/index.html

Lines changed: 85 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ <h1>JavaScript Test: 5</h1>
3030
==========================================================================================================
3131
*/
3232
// Q1. a
33-
const weightQa = (m, g = 10) => m * g;
33+
const weightQa = (m, g = 10) => (m * g).toFixed(2) + ' N';
3434
console.log(weightQa(100)); //Earth
3535
// 1000 N
3636
console.log(weightQa(100, 25)); //Jupiter
@@ -41,21 +41,31 @@ <h1>JavaScript Test: 5</h1>
4141
// 162 N
4242
// Q1. b
4343
const weightOfObject = (m, planet) => {
44+
let result;
4445
switch (planet.toLowerCase()) {
4546
case 'sun':
46-
return 274 * m + ' N';
47+
result = 274 * m ;
48+
break;
4749
case 'jupiter':
48-
return 25 * m + ' N';
50+
result = 25 * m ;
51+
break;
4952
case 'earth':
50-
return (9.79 * m).toFixed(2) + ' N';
53+
result = 9.79 * m ;
54+
break;
5155
case 'venus':
52-
return (8.87 * m).toFixed(2) + ' N';
56+
result = 8.87 * m ;
57+
break;
5358
case 'mars':
54-
return 3.7 * m + ' N';
59+
result = 3.7 * m ;
60+
break;
5561
case 'moon':
56-
return 1.62 * m + ' N';
62+
result = 1.62 * m ;
63+
break;
5764
default:
58-
}
65+
result = 'Not a valid input'
66+
};
67+
if (typeof result ==='number') return result.toFixed(2) + ' N';
68+
else result;
5969
};
6070

6171
console.log(weightOfObject(100, 'earth'));
@@ -69,7 +79,7 @@ <h1>JavaScript Test: 5</h1>
6979
==========================================================================================================
7080
*/
7181
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, '');
7383
console.log(cleanText(sentence));
7484
/*
7585
=============================== QUESTION 3 ===========================================================
@@ -82,9 +92,7 @@ <h1>JavaScript Test: 5</h1>
8292
let count = 0;
8393
const words = text.replace(/[,;#%&@!$'.?]/g, '').split(' ');
8494
for (const word of words) {
85-
if (word.length > 1) {
86-
count++;
87-
}
95+
if (word.length > 1) count++;
8896
}
8997

9098
return count;
@@ -96,14 +104,8 @@ <h1>JavaScript Test: 5</h1>
96104
======================================================================================================
97105
*/
98106
const countVarietyOfWords = sent => {
99-
let count = 0;
100107
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);
107109
return newWords.size;
108110
};
109111
console.log(countVarietyOfWords(sentence));
@@ -115,11 +117,13 @@ <h1>JavaScript Test: 5</h1>
115117
==========================================================================================================
116118
*/
117119
const supportWords = [
118-
'I',
120+
'i',
119121
'me',
120122
'mine',
123+
'my',
121124
'myself',
122125
'you',
126+
'your',
123127
'yours',
124128
'yourself',
125129
'he',
@@ -211,16 +215,13 @@ <h1>JavaScript Test: 5</h1>
211215
for (const word of words) {
212216
if (word.length > 1 && supportWords.indexOf(word.toLowerCase()) < 0) {
213217
newWords.push(word);
218+
count++;
214219
}
215220
}
216-
// console.log(Array.from(newWords));
217-
// console.log(newWords);
218-
219-
// return newWords.size;
220-
return newWords;
221+
return { words: newWords, count}
221222
};
222223
console.log(countOnlyMainWords(sentence));
223-
console.log(countOnlyMainWords(sentence));
224+
224225

225226
/*
226227
=============================== QUESTION 6 =================================================================
@@ -273,16 +274,14 @@ <h1>JavaScript Test: 5</h1>
273274
const stat = arr => {
274275
const mean = () => {
275276
let sum = 0;
276-
let average;
277277
for (item of arr) {
278278
sum += item;
279279
}
280-
average = sum / arr.length;
281-
return average;
280+
281+
return Math.round(sum / arr.length);
282282
};
283283
const median = () => {
284284
const arrangedArray = [...arr].sort((a, b) => a - b);
285-
console.log(arrangedArray);
286285
const index = arrangedArray.length / 2;
287286

288287
if (arrangedArray.length % 2 === 0) {
@@ -296,7 +295,6 @@ <h1>JavaScript Test: 5</h1>
296295
};
297296
const range = () => {
298297
const sortedArray = [...arr].sort((a, b) => a - b);
299-
console.log(sortedArray);
300298
const lastIndex = sortedArray.length - 1;
301299
return sortedArray[lastIndex] - sortedArray[0];
302300
};
@@ -328,7 +326,7 @@ <h1>JavaScript Test: 5</h1>
328326
};
329327

330328
return {
331-
mean: mean().toFixed(2),
329+
mean: mean(),
332330
median: median(),
333331
range: range(),
334332
mode: mode()
@@ -359,10 +357,10 @@ <h1>JavaScript Test: 5</h1>
359357
77.5,
360358
82.5
361359
];
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);
366364

367365
/*
368366
=============================== QUESTION 8 =================================================================
@@ -375,11 +373,9 @@ <h1>JavaScript Test: 5</h1>
375373
// generate array of rgb colors numbers.
376374

377375
const arrayOfRgbColors = (n = 1) => {
378-
let red;
379-
let green;
380-
let blue;
376+
let red, green, blue;
381377
let rgb;
382-
let rgbColors = [];
378+
const rgbColors = [];
383379
for (let i = 0; i < n; i++) {
384380
red = Math.floor(Math.random() * 256);
385381
green = Math.floor(Math.random() * 256);
@@ -391,6 +387,9 @@ <h1>JavaScript Test: 5</h1>
391387
return rgbColors;
392388
};
393389
console.log(arrayOfRgbColors());
390+
console.log(arrayOfRgbColors(2));
391+
console.log(arrayOfRgbColors(5));
392+
394393
// generate array of hexa colors numbers.
395394
const arrayOfHexaColors = (n = 1) => {
396395
const lettersNumbers = '0123456789abcdef';
@@ -409,6 +408,8 @@ <h1>JavaScript Test: 5</h1>
409408
return hexaColors;
410409
};
411410
console.log(arrayOfHexaColors());
411+
console.log(arrayOfHexaColors(2));
412+
console.log(arrayOfHexaColors(5));
412413

413414
/*
414415
=============================== QUESTION 9 =================================================================
@@ -418,7 +419,7 @@ <h1>JavaScript Test: 5</h1>
418419

419420
const arrayOfHexaOrRgb = (color = 'hexa', n = 1) => {
420421
let colors;
421-
let value = color.toLowerCase();
422+
const value = color.toLowerCase();
422423
if ('hexa' === value || 'hex' === value || 'hexadecimal' === value) {
423424
colors = arrayOfHexaColors(n);
424425
} else if ('rgb' === value || 'RGB' === value) {
@@ -452,19 +453,51 @@ <h1>JavaScript Test: 5</h1>
452453
c. Do you think there is similarity between the speeches ?
453454
============================================================================================================
454455
*/
455-
456-
console.log(countOnlyMainWords(melina));
457-
console.log(countOnlyMainWords(michelle));
458-
459-
const removeRepeatedWords = arr => {
456+
const removeRepeatedWords = arr => {
460457
const set = new Set(arr);
461458
return Array.from(set);
462459
};
463-
console.log(removeRepeatedWords(countOnlyMainWords(melina)).sort());
464-
console.log(removeRepeatedWords(countOnlyMainWords(michelle)).sort());
465460

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
468501
const checkSimilarity = (firstWords, secondWords) => {
469502
const commonWords = [];
470503
for (firstWord of firstWords) {
@@ -477,8 +510,8 @@ <h1>JavaScript Test: 5</h1>
477510
return commonWords;
478511
};
479512
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));
482515
</script>
483516
</body>
484517
</html>

0 commit comments

Comments
 (0)