-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount the Smiley Faces.js
More file actions
26 lines (22 loc) · 960 Bytes
/
Copy pathCount the Smiley Faces.js
File metadata and controls
26 lines (22 loc) · 960 Bytes
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
// COUNT THE SMILEY FACES www.codewars.com/kata/count-the-smiley-faces/train/javascript
// Description:
// 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:
// ;( :> :} :]
function countSmileys (arr) {
let smiles = 0;
for (let i in arr) {
!arr[i].match(/(.)\1+/) &&
(/([)D])/g).test(arr[i]) &&
arr[i].match(/^[-:;~)D]*$/) &&
arr[i].length <= 3 ? smiles += 1 : false;
}
return smiles;
}