3
3
The function countWords takes a paragraph and word as parameters.
4
4
============================================================================================= */
5
5
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.' ;
7
7
//Method one
8
8
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
10
10
const matches = para . match ( pattern ) || [ ] ; // if the para.match returns null, matches result will be en empty array
11
11
return matches . length ; // geting the length of the array
12
12
} ;
13
- console . log ( countWords1 ( paragraph , " love" ) ) ;
13
+ console . log ( countWords1 ( paragraph , ' love' ) ) ;
14
14
15
15
//Method
16
16
const countWords2 = ( para , wrd ) => {
17
17
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
19
19
//iterating through words array and checking if the target word is in the array
20
20
for ( const word of words ) {
21
21
//includes or startsWith could give the same result
@@ -26,12 +26,12 @@ const countWords2 = (para, wrd) => {
26
26
}
27
27
return count ;
28
28
} ;
29
- console . log ( countWords2 ( paragraph , " love" ) ) ;
29
+ console . log ( countWords2 ( paragraph , ' love' ) ) ;
30
30
31
31
/* ======================================== QUESTION 2 ======================================================
32
32
Write a function which takes an array as a parameter and returns an array of the data types of each item:
33
33
========================================================================================================== */
34
- const arr = [ " Asabeneh" , 100 , true , null , undefined , { job : " teaching" } ] ;
34
+ const arr = [ ' Asabeneh' , 100 , true , null , undefined , { job : ' teaching' } ] ;
35
35
const checkDataTypes = arr => {
36
36
const dataTypes = [ ] ; // creating an empty array
37
37
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));
79
79
/* ======================== QUESTION 5 ===========================
80
80
Write a function which remove an item or items from the middle of the array and replace with two items
81
81
================================================================ */
82
- const products = [ " Milk" , " Coffee" , " Tea" , " Honey" , " Meat" , " Cabage" ] ;
82
+ const products = [ ' Milk' , ' Coffee' , ' Tea' , ' Honey' , ' Meat' , ' Cabage' ] ;
83
83
const removeMiddleItem = ( arr , ...items ) => {
84
84
let middleIndex ; // to store the middle index of the array,
85
85
if ( arr . length % 2 === 0 ) {
@@ -96,7 +96,7 @@ const removeMiddleItem = (arr, ...items) => {
96
96
97
97
return arr ;
98
98
} ;
99
- console . log ( removeMiddleItem ( products , " potato" , " banana" ) ) ;
99
+ console . log ( removeMiddleItem ( products , ' potato' , ' banana' ) ) ;
100
100
101
101
/* ====================== QUESTION 6 ==============================================
102
102
Write a function which can generate a random Finnish car code(Car plate number).
@@ -107,10 +107,10 @@ console.log(removeMiddleItem(products, "potato", "banana"));
107
107
================================================================================ */
108
108
109
109
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
114
114
let indexOne ; // random index to extract one of the letter at a time from letters array
115
115
let indexTwo ; // random index to extract one of the number at a time from numbers array
116
116
for ( let i = 0 ; i < 3 ; i ++ ) {
@@ -135,18 +135,18 @@ console.log(genCarPlateNum());
135
135
removeProduct(3);
136
136
["Coffee", "Tea", "Sugar"]
137
137
================================================================================================= */
138
- const shoppingCart = [ " Milk" , " Coffee" , " Tea" , " Honey" ] ;
138
+ const shoppingCart = [ ' Milk' , ' Coffee' , ' Tea' , ' Honey' ] ;
139
139
const addProduct = ( products , product ) => {
140
140
products . push ( product ) ;
141
141
return products ;
142
142
} ;
143
- addProduct ( shoppingCart , " Meat" ) ;
143
+ addProduct ( shoppingCart , ' Meat' ) ;
144
144
console . log ( shoppingCart ) ;
145
145
const editProduct = ( products , index , product ) => {
146
146
products [ index ] = product ;
147
147
return products ;
148
148
} ;
149
- editProduct ( shoppingCart , 3 , " Sugar" ) ;
149
+ editProduct ( shoppingCart , 3 , ' Sugar' ) ;
150
150
console . log ( shoppingCart ) ;
151
151
const removeProduct = index => {
152
152
shoppingCart . splice ( index , 1 ) ;
@@ -165,25 +165,25 @@ console.log(shoppingCart);
165
165
190395 - 225J
166
166
============================================================================= */
167
167
const genSocialSecurityNum = ( ) => {
168
- const letters = " ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
169
- const numbers = " 0123456789" ;
168
+ const letters = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ' ;
169
+ const numbers = ' 0123456789' ;
170
170
let index = Math . floor ( Math . random ( ) * letters . length ) ;
171
171
const letter = letters [ index ] ; // getting a letter from the letters array
172
172
let date = Math . floor ( Math . random ( ) * 31 ) + 1 ; // date from 1 to 31
173
173
let month = Math . floor ( Math . random ( ) * 12 ) + 1 ; // month from 1 to 12
174
174
175
175
//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 ;
178
178
179
179
let year = Math . floor ( Math . random ( ) * 2019 ) ;
180
180
if ( year > 100 ) {
181
181
year = year . toString ( ) . substr ( - 2 ) ;
182
182
} else if ( year < 10 ) {
183
- year = "0" + year ;
183
+ year = '0' + year ;
184
184
}
185
185
186
- let suffix = "" ;
186
+ let suffix = '' ;
187
187
for ( let i = 0 ; i < 3 ; i ++ ) {
188
188
let randomIndex = Math . floor ( Math . random ( ) * numbers . length ) ;
189
189
suffix += numbers [ randomIndex ] ;
@@ -218,18 +218,18 @@ console.log(genSocialSecurityNum());
218
218
========================================================================================================= */
219
219
const todoList = [
220
220
{
221
- task : " Prepare JS Test" ,
222
- time : " 4/3/2019 8:30" ,
221
+ task : ' Prepare JS Test' ,
222
+ time : ' 4/3/2019 8:30' ,
223
223
completed : true
224
224
} ,
225
225
{
226
- task : " Give JS Test" ,
227
- time : " 4/3/2019 10:00" ,
226
+ task : ' Give JS Test' ,
227
+ time : ' 4/3/2019 10:00' ,
228
228
completed : false
229
229
} ,
230
230
{
231
- task : " Assess Test Result" ,
232
- time : " 4/3/2019 1:00" ,
231
+ task : ' Assess Test Result' ,
232
+ time : ' 4/3/2019 1:00' ,
233
233
completed : false
234
234
}
235
235
] ;
@@ -243,7 +243,7 @@ const displayDateTime = () => {
243
243
const hours = now . getHours ( ) ;
244
244
let minutes = now . getMinutes ( ) ;
245
245
if ( minutes < 10 ) {
246
- minutes = "0" + minutes ;
246
+ minutes = '0' + minutes ;
247
247
}
248
248
return `${ date } /${ month } /${ year } ${ hours } :${ minutes } ` ;
249
249
} ;
@@ -339,45 +339,45 @@ console.log(shuffle([1, 2, 3, 4, 5]));
339
339
========================================================================================== */
340
340
const users = [
341
341
{
342
- name : " Brook" ,
342
+ name : ' Brook' ,
343
343
scores : 75 ,
344
- skills : [ " HTM" , " CSS" , "JS" ] ,
344
+ skills : [ ' HTM' , ' CSS' , 'JS' ] ,
345
345
age : 16
346
346
} ,
347
347
{
348
- name : " Alex" ,
348
+ name : ' Alex' ,
349
349
scores : 80 ,
350
- skills : [ " HTM" , " CSS" , "JS" ] ,
350
+ skills : [ ' HTM' , ' CSS' , 'JS' ] ,
351
351
age : 18
352
352
} ,
353
353
{
354
- name : " David" ,
354
+ name : ' David' ,
355
355
scores : 75 ,
356
- skills : [ " HTM" , " CSS" ] ,
356
+ skills : [ ' HTM' , ' CSS' ] ,
357
357
age : 22
358
358
} ,
359
359
{
360
- name : " John" ,
360
+ name : ' John' ,
361
361
scores : 85 ,
362
- skills : [ " HTM" ] ,
362
+ skills : [ ' HTM' ] ,
363
363
age : 25
364
364
} ,
365
365
{
366
- name : " Sara" ,
366
+ name : ' Sara' ,
367
367
scores : 95 ,
368
- skills : [ " HTM" , " CSS" , "JS" ] ,
368
+ skills : [ ' HTM' , ' CSS' , 'JS' ] ,
369
369
age : 26
370
370
} ,
371
371
{
372
- name : " Martha" ,
372
+ name : ' Martha' ,
373
373
scores : 80 ,
374
- skills : [ " HTM" , " CSS" , "JS" ] ,
374
+ skills : [ ' HTM' , ' CSS' , 'JS' ] ,
375
375
age : 18
376
376
} ,
377
377
{
378
- name : " Thomas" ,
378
+ name : ' Thomas' ,
379
379
scores : 90 ,
380
- skills : [ " HTM" , " CSS" , "JS" ] ,
380
+ skills : [ ' HTM' , ' CSS' , 'JS' ] ,
381
381
age : 20
382
382
}
383
383
] ;
@@ -393,49 +393,49 @@ const scoresGreaterThan85 = arr => {
393
393
} ;
394
394
395
395
console . log ( scoresGreaterThan85 ( users ) ) ;
396
- const user = {
397
- name : " Asabeneh" ,
396
+ const newUser = {
397
+ name : ' Asabeneh' ,
398
398
scores : 800 ,
399
- skills : [ " HTML" , " CSS" , "JS" ] ,
399
+ skills : [ ' HTML' , ' CSS' , 'JS' ] ,
400
400
age : 250
401
401
} ;
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' ;
406
406
}
407
407
}
408
- arr . push ( user ) ;
408
+ arr . push ( newUser ) ;
409
409
return arr ;
410
410
} ;
411
- console . log ( addUser ( users , user ) ) ;
411
+ console . log ( addUser ( users , newUser ) ) ;
412
412
413
413
const addUserSkill = ( arr , name , skill ) => {
414
414
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 ) ;
418
418
found = true ;
419
419
break ;
420
- }
421
- }
422
- if ( ! found ) {
423
- return "A user does not exist" ;
424
420
}
421
+ }
422
+ if ( ! found ) {
423
+ return 'A user does not exist' ;
424
+ }
425
425
426
426
return arr ;
427
427
} ;
428
- console . log ( addUserSkill ( users , " Brook" , " Node" ) ) ;
428
+ console . log ( addUserSkill ( users , ' Brook' , ' Node' ) ) ;
429
429
430
430
const editUser = ( arr , name , newUser ) => {
431
431
for ( const user of arr ) {
432
- if ( user [ " name" ] === name ) {
432
+ if ( user [ ' name' ] === name ) {
433
433
user . name = newUser . name ;
434
434
user . scores = newUser . scores ;
435
435
user . skills = newUser . skills ;
436
436
user . age = newUser . age ;
437
437
} else {
438
- return " A user does not exist" ;
438
+ return ' A user does not exist' ;
439
439
}
440
440
}
441
441
0 commit comments