-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution0089.js
49 lines (35 loc) · 1.28 KB
/
solution0089.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
/*
*Repeat- Part Of String Methods Series*
--------------- 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: ;( :> :} :]
-------------
Sample Tests
function countSmileys(arr) {
return arr.filter(x => /^[:;][-~]?[)D]$/.test(x)).length;
}
--------------
PREP
Parameters: An array of 'smiley face-esque' character collections
Return: The total number of actual smiley faces
Example: [':D',':~)',';~D',':)'] => 4
Psuedo Code:
-use .filter() to return array
-filter for regex statement
- regex = /^ ... $/, start to finish
[:;]
[-~]
? (greedy quantifier, matches 0 or 1 times, as many times as possible )
[)D]
-.test(array).length
*/
function countSmileys(arr) {
return arr.filter(x => /^[:;][-~]?[)D]$/.test(x)).length;
}