Skip to content

Instantly share code, notes, and snippets.

@jerolan
Created September 25, 2019 03:49
Show Gist options
  • Save jerolan/ea7b8fdf9d97e83e98c1f92d1c7c7c30 to your computer and use it in GitHub Desktop.
Save jerolan/ea7b8fdf9d97e83e98c1f92d1c7c7c30 to your computer and use it in GitHub Desktop.
const regex = /\W|_/g;
function longest(acc, elem) {
if (acc.length < elem.length && !regex.test(elem)) return elem;
return acc
}
function longestWord(sen) {
return sen.split(" ").reduce(longest, "");
}
@jerolan
Copy link
Author

jerolan commented Sep 25, 2019

function longestWord(sen) {
  const regex = /\W|_/g;
  const arr = sen.split(" ");
  let word = "";

  for (let i = 0; i < arr.length; i++) {
    const currentWord = arr[i];
    if (word.length < currentWord.length && !regex.test(currentWord))
      word = currentWord;
  }

  return word;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment