Last active
April 21, 2016 15:36
-
-
Save skylerto/7c9814258a7a3f03ea44ec292b98d19b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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