-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution0084.js
51 lines (34 loc) · 1.88 KB
/
solution0084.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
41
42
43
44
45
46
47
48
49
/*
--------------- 6 Kyu - Count the smiley faces! ------------------
Instructions:
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
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.
-------------
Sample Tests
--------------
PREP
Parameters: an array of strings containing hte following characters: ':', ';', '-', '~', ')', 'D'.
Return: numer of array elements that consist of 'smily faces', beginning with one of the first two characters, followed by either of the second two and then wither of the final two, or the first two then final two.
Example: countSmileys([':)', ';(', ';}', ':-D']); // should return 2;
Psuedo Code:
-filter(x =>) array, with /regex/.test()
.test() will return true for successfull regex match
-? makes the previous match optional.
-^ means beginning of string/line and $ means end of string/line
-so /^[:;][-~]?[)D]$ .. should match/true, from beginning to end of string, smiley 'eyes', 'nose' optional, smiley 'mouth'
*/
function countSmileys(arr) {
return arr.filter(x => /^[:;][-~]?[)D]$/.test(x)).length;
}