DEV Community

Author
Author

Posted on

A Ruby/JS Equivalence

This is a Ruby function

def new_count(word)
  word.downcase!
  return 1 if word.length <= 3
  word.sub!(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '')
  word.sub!(/^y/, '')
  word.scan(/[aeiouy]{1,2}/).size
end
Enter fullscreen mode Exit fullscreen mode

This is an equivalent function in JavaScript.

function newCount(word) {
  word = word.toLowerCase();
  if (word.length <= 3) return 1;
  word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '');
  word = word.replace(/^y/, '');
  return word.match(/[aeiouy]{1,2}/g).length;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)