challenge : https://www.codewars.com/kata/583203e6eb35d7980400002a
I’m wondering if anyone know how to solve this challenge with regular expressions? because in the challenge, it has a “regular expression” tag attached to it.
i passed this with my code :
//return the total number of smiling faces in the array
function countSmileys(arr) {
if ( arr.length === 0 ) {
return 0;
}
let incr = 0;
let validSmileys = [":)",";)",":D",";D",":-D",":~D",":-)",":~)",";~D",";~)",";-D",";-)"]
for (let face of arr) {
if (validSmileys.includes(face)) {
incr++;
}
}
return incr;
}
countSmileys([";~D" , ":o>" , ";~)" , ":)" , ";~>" , ":(" , ";D"])
// : or ;
// - or ~
// ) or D.
I learned regex but its a bit cloudy for me since i don’t use it much. your example reinforced the practicality of them for me. as for the OOP example, i tried and they worked but im wondering though, shouldn’t they return an array of true values? like [true,true…] since validSmileys[chars] gives the value of the property which is true. that’s the only thing im confused of really.
I used test method instead of match and it works fine too.
function countSmileys(arr) {
let faceRegex= /[;:][~-]?[)D]/
let incr = 0;
for (face of arr) {
if (faceRegex.test(face)) {
incr++;
}
}
return incr;
}
countSmileys([':)',':(',':D',':O',':;'])