Skip to content

Instantly share code, notes, and snippets.

@skylerto
Last active April 21, 2016 15:36
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 skylerto/7c9814258a7a3f03ea44ec292b98d19b to your computer and use it in GitHub Desktop.
Save skylerto/7c9814258a7a3f03ea44ec292b98d19b to your computer and use it in GitHub Desktop.
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// If there are no sentences get out quick.
if(S.length <= 0){
return 0;
}
// Split the sentence up over ., !, or ?.
var sentences = S.split(/[.!?]/);
// If the sentences are
if (sentences.length === 1){
return sentences.length;
}
// Split up the sentences into words.
sentences.forEach(function(sentence, index) {
var words = sentence.split(' ');
// Clean up the words array (get rid of the junk "words").
words.forEach(function(word){
word = word.trim();
if(word.match(/\s+/g) || word.length <= 0 || word.match(/\n+/g)){
words.splice(words.indexOf(word), 1);
}
});
sentences[index] = words; // re-assign the words.
});
// Get the count of the sentences.
var max = 0;
sentences.forEach(function(sentence) {
if(sentence.length >= max){
max = sentence.length;
}
});
return max;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment