I'm working through some beginner Coderbyte problems and I've e across an interesting dilemma. Here's the problem:
"Using the JavaScript language, have the function LongestWord(sen) take the sen parameter being passed and return the largest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty."
Here's my code:
function LongestWord(sen) {
var myArray = sen.split(" ");
var lengthOfSubstring = 0;
for (var i = 0; i < myArray.length; i++) {
if (myArray[i].length > lengthOfSubstring) {
lengthOfSubstring = myArray[i].length;
sen = myArray[i];
}
}
return sen;
}
console.log(LongestWord("Argument goes here"));
My code passes every test unless the argument contains punctuation. Is there anyway to remove or ignore it? Every search brings up regex and very intimidating syntax haha
EDIT: used the match() method on the sen parameter courtesy of @Markus
function LongestWord(sen) {
var myArray = sen.match(/[a-z]+/gi);
var lengthOfSubstring = 0;
for (var i = 0; i < myArray.length; i++) {
if (myArray[i].length > lengthOfSubstring) {
lengthOfSubstring = myArray[i].length;
sen = myArray[i];
}
}
return sen;
}
console.log(LongestWord("Argument goes here"));
I'm working through some beginner Coderbyte problems and I've e across an interesting dilemma. Here's the problem:
"Using the JavaScript language, have the function LongestWord(sen) take the sen parameter being passed and return the largest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty."
Here's my code:
function LongestWord(sen) {
var myArray = sen.split(" ");
var lengthOfSubstring = 0;
for (var i = 0; i < myArray.length; i++) {
if (myArray[i].length > lengthOfSubstring) {
lengthOfSubstring = myArray[i].length;
sen = myArray[i];
}
}
return sen;
}
console.log(LongestWord("Argument goes here"));
My code passes every test unless the argument contains punctuation. Is there anyway to remove or ignore it? Every search brings up regex and very intimidating syntax haha
EDIT: used the match() method on the sen parameter courtesy of @Markus
function LongestWord(sen) {
var myArray = sen.match(/[a-z]+/gi);
var lengthOfSubstring = 0;
for (var i = 0; i < myArray.length; i++) {
if (myArray[i].length > lengthOfSubstring) {
lengthOfSubstring = myArray[i].length;
sen = myArray[i];
}
}
return sen;
}
console.log(LongestWord("Argument goes here"));
Share
Improve this question
edited Apr 26, 2015 at 22:11
jakewies
asked Apr 26, 2015 at 21:38
jakewiesjakewies
4621 gold badge8 silver badges19 bronze badges
10
-
1
str.replace(/\./g, ' ')
– adeneo Commented Apr 26, 2015 at 21:39 - 2 Don't be intimidated by regex. Embrace it. The alternatives are much uglier and intimidating. – ThisClark Commented Apr 26, 2015 at 21:42
- 1 @adeneo — The question says besides regex (and punctuation enpasses many more characters than the full stop). – Quentin Commented Apr 26, 2015 at 21:42
- Regex is pretty damn fast, super pact and will save you hundreds of lines of coding down the road. And it's not even that hard to learn. Don't be intimidated by it! Even after learning just the basics, several string-related problems will bee trivial to you. – Shashank Commented Apr 26, 2015 at 21:48
- 2 And no, regex is not the only way to achieve this, you could split all the characters into an array, iterate, check for certain characters, and remove each one, but why? – adeneo Commented Apr 26, 2015 at 21:50
3 Answers
Reset to default 2Without regex:
function LongestWord(sen) {
var wordStart = -1;
var bestWord = null;
var bestLength = 0;
for (var i = 0; i < sen.length; i++) {
var ch = sen[i];
if ('a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z')
{
if (wordStart === -1)
{
wordStart = i;
}
}
else
{
if (wordStart !== -1)
{
var word = sen.substring(wordStart, i);
if (word.length > bestLength)
{
bestLength = word.length;
bestWord = word;
}
wordStart = -1;
}
}
}
if (wordStart !== -1)
{
var word = sen.substring(wordStart);
if (word.length > bestLength)
{
bestLength = word.length;
bestWord = word;
}
wordStart = -1;
}
return bestWord;
}
With regex:
function LongestWord(sen) {
var bestWord = null;
var bestLength = 0;
var matches = sen.match(/[a-z]+/gi);
for (var i = 0; i < matches.length; i++)
var word = matches[i];
if (word.Length > bestLength)
{
bestLength = word.Length;
bestWord = word;
}
}
return bestWord;
}
Without regex
function LongestWord(sen) {
var punct = '\.,-/#!$%^&*;:{}=-_`~()'.split('');
var words = sen.split(" ").map(function(item) {
return item.split('').filter(function(char) {
return punct.indexOf(char) === -1;
}).join('');
});
return words.reduce(function(a, b) {
return a.length > b.length ? a : b;
});
}
With regex
function LongestWord(sen) {
return sen.split(" ").map(function(word) {
return word.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/gi, '');
}).reduce(function(a, b) {
return a.length > b.length ? a : b;
});
}
Here is one way to do it without regex using a filter function that is pretty pact:
function longestWord(sen) {
// We are using Python's string.punctuation set.
var punct = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';
var wordArr = sen.split(' ');
var filteredWordArr = wordArr.map(function (word) {
return word.split('').filter(function (ch) {
return punct.indexOf(ch) < 0;
}).join('');
});
return wordArr[filteredWordArr.reduce(function (maxIdx, currWord, i, fwa) {
return currWord.length > fwa[maxIdx].length ? i : maxIdx;
}, 0)];
}
console.log(longestWord("Çüéâäâ, wouldn't you like to play?")); // wouldn't
This function returns the unfiltered word that has the greatest length after being filtered and pared to other filtered words, which is probably what you want. I noticed that an older version of this code was returning "wouldnt" instead of "wouldn't", but it's fixed now.
Using regex does clean up this code up a bit though. It helps you replace the split, filter, and join operations with a single regex replace operation:
function longestWord(sen) {
var punct = /[!"#$%&'()*+,\-.\/\\:;<=>?@[\]^_`{\|}~]/g;
var wordArr = sen.split(' ');
var filteredWordArr = wordArr.map(function (word) {
return word.replace(punct, '');
});
return wordArr[filteredWordArr.reduce(function (maxIdx, currWord, i, fwa) {
return currWord.length > fwa[maxIdx].length ? i : maxIdx;
}, 0)];
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744890276a4599368.html
评论列表(0条)