Skip to content

Commit fe89c60

Browse files
committed
refactore
1 parent 1bb2750 commit fe89c60

File tree

1 file changed

+61
-61
lines changed

1 file changed

+61
-61
lines changed

solutions/javascript-test-3.js

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
The function countWords takes a paragraph and word as parameters.
44
============================================================================================= */
55
const paragraph =
6-
"I love teaching. If you do not love teaching what else can you love. I love JavaScript if you do not love something which can give life to your application what else can you love.";
6+
'I love teaching. If you do not love teaching what else can you love. I love JavaScript if you do not love something which can give life to your application what else can you love.';
77
//Method one
88
const countWords1 = (para, wrd) => {
9-
const pattern = new RegExp(wrd, "gi"); //creating regex object using RegExp constructor
9+
const pattern = new RegExp(wrd, 'gi'); //creating regex object using RegExp constructor
1010
const matches = para.match(pattern) || []; // if the para.match returns null, matches result will be en empty array
1111
return matches.length; // geting the length of the array
1212
};
13-
console.log(countWords1(paragraph, "love"));
13+
console.log(countWords1(paragraph, 'love'));
1414

1515
//Method
1616
const countWords2 = (para, wrd) => {
1717
let count = 0;
18-
const words = para.split(" "); // spliting the paragraph into array of words
18+
const words = para.split(' '); // spliting the paragraph into array of words
1919
//iterating through words array and checking if the target word is in the array
2020
for (const word of words) {
2121
//includes or startsWith could give the same result
@@ -26,12 +26,12 @@ const countWords2 = (para, wrd) => {
2626
}
2727
return count;
2828
};
29-
console.log(countWords2(paragraph, "love"));
29+
console.log(countWords2(paragraph, 'love'));
3030

3131
/* ======================================== QUESTION 2 ======================================================
3232
Write a function which takes an array as a parameter and returns an array of the data types of each item:
3333
========================================================================================================== */
34-
const arr = ["Asabeneh", 100, true, null, undefined, { job: "teaching" }];
34+
const arr = ['Asabeneh', 100, true, null, undefined, { job: 'teaching' }];
3535
const checkDataTypes = arr => {
3636
const dataTypes = []; // creating an empty array
3737
let type; // will be used in the loop to store the data type of each element in the array
@@ -79,7 +79,7 @@ console.log(averageAge(ages));
7979
/* ======================== QUESTION 5 ===========================
8080
Write a function which remove an item or items from the middle of the array and replace with two items
8181
================================================================ */
82-
const products = ["Milk", "Coffee", "Tea", "Honey", "Meat", "Cabage"];
82+
const products = ['Milk', 'Coffee', 'Tea', 'Honey', 'Meat', 'Cabage'];
8383
const removeMiddleItem = (arr, ...items) => {
8484
let middleIndex; // to store the middle index of the array,
8585
if (arr.length % 2 === 0) {
@@ -96,7 +96,7 @@ const removeMiddleItem = (arr, ...items) => {
9696

9797
return arr;
9898
};
99-
console.log(removeMiddleItem(products, "potato", "banana"));
99+
console.log(removeMiddleItem(products, 'potato', 'banana'));
100100

101101
/* ====================== QUESTION 6 ==============================================
102102
Write a function which can generate a random Finnish car code(Car plate number).
@@ -107,10 +107,10 @@ console.log(removeMiddleItem(products, "potato", "banana"));
107107
================================================================================ */
108108

109109
const genCarPlateNum = () => {
110-
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
111-
const numbers = "0123456789";
112-
let lettersPart = ""; // variable to store, the letters part
113-
let numbersPart = ""; // variable to store, the letters part
110+
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
111+
const numbers = '0123456789';
112+
let lettersPart = ''; // variable to store, the letters part
113+
let numbersPart = ''; // variable to store, the letters part
114114
let indexOne; // random index to extract one of the letter at a time from letters array
115115
let indexTwo; // random index to extract one of the number at a time from numbers array
116116
for (let i = 0; i < 3; i++) {
@@ -135,18 +135,18 @@ console.log(genCarPlateNum());
135135
removeProduct(3);
136136
["Coffee", "Tea", "Sugar"]
137137
================================================================================================= */
138-
const shoppingCart = ["Milk", "Coffee", "Tea", "Honey"];
138+
const shoppingCart = ['Milk', 'Coffee', 'Tea', 'Honey'];
139139
const addProduct = (products, product) => {
140140
products.push(product);
141141
return products;
142142
};
143-
addProduct(shoppingCart, "Meat");
143+
addProduct(shoppingCart, 'Meat');
144144
console.log(shoppingCart);
145145
const editProduct = (products, index, product) => {
146146
products[index] = product;
147147
return products;
148148
};
149-
editProduct(shoppingCart, 3, "Sugar");
149+
editProduct(shoppingCart, 3, 'Sugar');
150150
console.log(shoppingCart);
151151
const removeProduct = index => {
152152
shoppingCart.splice(index, 1);
@@ -165,25 +165,25 @@ console.log(shoppingCart);
165165
190395 - 225J
166166
============================================================================= */
167167
const genSocialSecurityNum = () => {
168-
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
169-
const numbers = "0123456789";
168+
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
169+
const numbers = '0123456789';
170170
let index = Math.floor(Math.random() * letters.length);
171171
const letter = letters[index]; // getting a letter from the letters array
172172
let date = Math.floor(Math.random() * 31) + 1; // date from 1 to 31
173173
let month = Math.floor(Math.random() * 12) + 1; // month from 1 to 12
174174

175175
//if the date or month is less than 10
176-
if (date < 10) date = "0" + date;
177-
if (month < 10) month = "0" + month;
176+
if (date < 10) date = '0' + date;
177+
if (month < 10) month = '0' + month;
178178

179179
let year = Math.floor(Math.random() * 2019);
180180
if (year > 100) {
181181
year = year.toString().substr(-2);
182182
} else if (year < 10) {
183-
year = "0" + year;
183+
year = '0' + year;
184184
}
185185

186-
let suffix = "";
186+
let suffix = '';
187187
for (let i = 0; i < 3; i++) {
188188
let randomIndex = Math.floor(Math.random() * numbers.length);
189189
suffix += numbers[randomIndex];
@@ -218,18 +218,18 @@ console.log(genSocialSecurityNum());
218218
========================================================================================================= */
219219
const todoList = [
220220
{
221-
task: "Prepare JS Test",
222-
time: "4/3/2019 8:30",
221+
task: 'Prepare JS Test',
222+
time: '4/3/2019 8:30',
223223
completed: true
224224
},
225225
{
226-
task: "Give JS Test",
227-
time: "4/3/2019 10:00",
226+
task: 'Give JS Test',
227+
time: '4/3/2019 10:00',
228228
completed: false
229229
},
230230
{
231-
task: "Assess Test Result",
232-
time: "4/3/2019 1:00",
231+
task: 'Assess Test Result',
232+
time: '4/3/2019 1:00',
233233
completed: false
234234
}
235235
];
@@ -243,7 +243,7 @@ const displayDateTime = () => {
243243
const hours = now.getHours();
244244
let minutes = now.getMinutes();
245245
if (minutes < 10) {
246-
minutes = "0" + minutes;
246+
minutes = '0' + minutes;
247247
}
248248
return `${date}/${month}/${year} ${hours}:${minutes}`;
249249
};
@@ -339,45 +339,45 @@ console.log(shuffle([1, 2, 3, 4, 5]));
339339
========================================================================================== */
340340
const users = [
341341
{
342-
name: "Brook",
342+
name: 'Brook',
343343
scores: 75,
344-
skills: ["HTM", "CSS", "JS"],
344+
skills: ['HTM', 'CSS', 'JS'],
345345
age: 16
346346
},
347347
{
348-
name: "Alex",
348+
name: 'Alex',
349349
scores: 80,
350-
skills: ["HTM", "CSS", "JS"],
350+
skills: ['HTM', 'CSS', 'JS'],
351351
age: 18
352352
},
353353
{
354-
name: "David",
354+
name: 'David',
355355
scores: 75,
356-
skills: ["HTM", "CSS"],
356+
skills: ['HTM', 'CSS'],
357357
age: 22
358358
},
359359
{
360-
name: "John",
360+
name: 'John',
361361
scores: 85,
362-
skills: ["HTM"],
362+
skills: ['HTM'],
363363
age: 25
364364
},
365365
{
366-
name: "Sara",
366+
name: 'Sara',
367367
scores: 95,
368-
skills: ["HTM", "CSS", "JS"],
368+
skills: ['HTM', 'CSS', 'JS'],
369369
age: 26
370370
},
371371
{
372-
name: "Martha",
372+
name: 'Martha',
373373
scores: 80,
374-
skills: ["HTM", "CSS", "JS"],
374+
skills: ['HTM', 'CSS', 'JS'],
375375
age: 18
376376
},
377377
{
378-
name: "Thomas",
378+
name: 'Thomas',
379379
scores: 90,
380-
skills: ["HTM", "CSS", "JS"],
380+
skills: ['HTM', 'CSS', 'JS'],
381381
age: 20
382382
}
383383
];
@@ -393,49 +393,49 @@ const scoresGreaterThan85 = arr => {
393393
};
394394

395395
console.log(scoresGreaterThan85(users));
396-
const user = {
397-
name: "Asabeneh",
396+
const newUser = {
397+
name: 'Asabeneh',
398398
scores: 800,
399-
skills: ["HTML", "CSS", "JS"],
399+
skills: ['HTML', 'CSS', 'JS'],
400400
age: 250
401401
};
402-
const addUser = (arr, user) => {
403-
for (let i = 0; i < arr.length; i++) {
404-
if (arr[i]["name"] === user.name) {
405-
return " A user does exist";
402+
const addUser = (arr, newUser) => {
403+
for (const user of arr) {
404+
if (user['name'] === newUser.name) {
405+
return ' A user does exist';
406406
}
407407
}
408-
arr.push(user);
408+
arr.push(newUser);
409409
return arr;
410410
};
411-
console.log(addUser(users, user));
411+
console.log(addUser(users, newUser));
412412

413413
const addUserSkill = (arr, name, skill) => {
414414
let found = false;
415-
for (let i = 0; i < arr.length; i++) {
416-
if (arr[i]["name"] === name) {
417-
arr[i].skills.push(skill);
415+
for (const user of arr) {
416+
if (user['name'] === name) {
417+
user.skills.push(skill);
418418
found = true;
419419
break;
420-
}
421-
}
422-
if(!found) {
423-
return "A user does not exist";
424420
}
421+
}
422+
if (!found) {
423+
return 'A user does not exist';
424+
}
425425

426426
return arr;
427427
};
428-
console.log(addUserSkill(users, "Brook", "Node"));
428+
console.log(addUserSkill(users, 'Brook', 'Node'));
429429

430430
const editUser = (arr, name, newUser) => {
431431
for (const user of arr) {
432-
if (user["name"] === name) {
432+
if (user['name'] === name) {
433433
user.name = newUser.name;
434434
user.scores = newUser.scores;
435435
user.skills = newUser.skills;
436436
user.age = newUser.age;
437437
} else {
438-
return "A user does not exist";
438+
return 'A user does not exist';
439439
}
440440
}
441441

0 commit comments

Comments
 (0)