Skip to content

Instantly share code, notes, and snippets.

@nkhil
Last active August 25, 2020 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkhil/99a931c025890795285eaf7d0dc79088 to your computer and use it in GitHub Desktop.
Save nkhil/99a931c025890795285eaf7d0dc79088 to your computer and use it in GitHub Desktop.

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: ;( :> :} :]

Regex solution

function countSmileys(arr) {
  const SMILEYS = /^[:;][-~]?[\)D]$/
  return arr.filter(smiley => SMILEYS.test(smiley)).length
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment