-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountTheSmileyFaces.js
40 lines (26 loc) · 1.27 KB
/
countTheSmileyFaces.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
Codewars: Count the Smiley faces
https://www.codewars.com/kata/583203e6eb35d7980400002a
Given an array (arr) as an argument complete the function countSmileys that should return the total number of smiling faces.
Rules for a smiling face:
-Each smiley face must contain a valid pair of eyes. Eyes can be marked as : or ;
-A smiley face can have a nose but it does not have to. Valid characters for a nose are - or ~
-Every smiling face must have a smiling mouth that should be marked with either ) or D.
No additional characters are allowed except for those mentioned.
Valid smiley face examples:
:) :D ;-D :~)
Invalid smiley faces:
;( :> :} :]
Example cases:
countSmileys([':)', ';(', ';}', ':-D']); // should return 2;
countSmileys([';D', ':-(', ':-)', ';~)']); // should return 3;
countSmileys([';]', ':[', ';*', ':$', ';-D']); // should return 1;
Note: In case of an empty array return 0. You will not be tested with invalid input (input will always be an array). Order of the face (eyes, nose, mouth) elements will always be the same
//*
// Personal comments: I love smiley faces
function countSmileys(arr) {
let reg = /(^:|;)(-?|~?)(\)$|D$)/;
let count=0;
arr.forEach(item => {if(reg.test(item)) count++});
return count;
}