-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
count-the-smiley-faces.js
105 lines (91 loc) · 2.36 KB
/
count-the-smiley-faces.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// -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:
// ;( :> :} :]
//return the total number of smiling faces in the array
function countSmileys(arr) {
// a place to keep track of the current count
let count = 0;
// iterate over the array
for (let i = 0; i < arr.length; i++) {
// iterate over the current face if the length of the face is either 2 or 3
const face = arr[i];
if (face.length == 2 || face.length == 3) {
// if the first character is a : ;
if (face[0] == ':' || face[0] == ';') {
// if the second character is a - ~
if (face[1] == '-' || face[1] == '~') {
// if the third character is a ) D
if (face[2] == ')' || face[2] == 'D') {
// increment count
count++;
}
} else if ((face[1] == ')' || face[1] == 'D') && face.length == 2) {
// else if the second character is a ) D && the length is 2
// increment count
count++;
}
}
}
}
// return count
return count;
}
function countSmileys(arr) {
let count = 0;
const eyes = {
':': true,
';': true
};
const noses = {
'-': true,
'~': true
};
const mouths = {
')': true,
'D': true
};
for (let i = 0; i < arr.length; i++) {
const face = arr[i];
if (face.length == 2) {
if (eyes[face[0]] && mouths[face[1]]) {
count++;
}
} else if (face.length == 3) {
if (eyes[face[0]] && noses[face[1]] && mouths[face[2]]) {
count++;
}
}
}
return count;
}
function countSmileys(arr) {
const eyes = {
':': true,
';': true
};
const noses = {
'-': true,
'~': true
};
const mouths = {
')': true,
'D': true
};
return arr.reduce((count, face) => {
if (face.length == 2) {
if (eyes[face[0]] && mouths[face[1]]) {
count++;
}
} else if (face.length == 3) {
if (eyes[face[0]] && noses[face[1]] && mouths[face[2]]) {
count++;
}
}
return count;
}, 0);
}