2

My desired result is sentence but it returns sentence.. How do I specify that I want to eliminate the '.'?

function longestWord(string) {
  var str = string.split(" ");
  var longest = 0;
  var word = null;
  var sep = '.,?!';
  for (var i = 0; i < str.length; i++) {
    if (sep.includes(str[i])) {
      str[i] = str[i].substring(0, str[i].length - 1);
    }
    if (longest < str[i].length) {
      longest = str[i].length;
      word = str[i];
    }
  }
  return word;
}

console.log(longestWord('Create a function to return the longest word(s) in a sentence.'));
mplungjan
  • 169,008
  • 28
  • 173
  • 236
JuanDraper
  • 49
  • 5
  • 2
    `sep.includes(str[i])` checks if the string `'.,?!'` has the full word `str[i]` in it. Instead of using `split`, you can simply use `var str = string.match(/\b(\w+)\b/g)` to directly get the words. This will remove the comma, dots etc. Then you woudn't need the `sep` logic at all: [JavaScript break sentence by words](https://stackoverflow.com/a/36508315) – adiga Jan 15 '21 at 09:16
  • 1
    On your example you have two words that satisfy your criteria - they're the longest ones with the same length - 8 chars. *function* and *sentence*. What would be the expected result? *Sentence.* isn't really a word. It's a word followed by punctuation. – emerson.marini Jan 15 '21 at 09:21
  • In your example you have `word(s)`; how should it be considered? Should it be classified as a 4, 5 or 7 characters word? – secan Jan 15 '21 at 09:29

4 Answers4

4

Regex would work better

const nonAlpha = /[^\w\s]|_/g  // non alphabetic and underscore

const longestWord = string => string
   .replace(nonAlpha,"").split(" ")    // remove non alphabetics    
   .sort((a,b) => a.length - b.length) // sort on length
   .pop();                             // return the longest


console.log(longestWord('Create a function to return the longest word(s) in a sentence.'));
mplungjan
  • 169,008
  • 28
  • 173
  • 236
3

Tweaks at your existing solution

You could use replace by regex to exclude sep

Moreover, the if (longest < str[i].length) { should also be changed to <= so you will have the latest longest word, because function also is the same lenghth as sentence

function longestWord(string) {
  var str = string.split(" ");
  var longest = 0;
  var word = null;
  var sep = ".,?!";
  var regex = new RegExp(`[${sep}]`, "g");
  for (var i = 0; i < str.length; i++) {
    str[i] = str[i].replace(regex, "");
    if (longest <= str[i].length) {
      longest = str[i].length;
      word = str[i];
    }
  }
  return word;
}

console.log(
  longestWord("Create a function to return the longest word(s) in a sentence.")
);
hgb123
  • 13,869
  • 3
  • 20
  • 38
1

Your include condition doesn't work as expected.

function longestWord(string) {
  var str = string.split(" ");
  var longest = 0;
  var word = null;
  var sep = '.,?!';
  for (var i = 0; i < str.length; i++) {
    if (str[i].split('').some(v => sep.split('').includes(v))) {
      str[i] = str[i].substring(0, str[i].length - 1);
    }
    if (longest <= str[i].length) {
      longest = str[i].length;
      word = str[i];
    }
  }
  return word;
}

console.log(longestWord('Create a function to return the longest word(s) in a sentence.'));

"function" and "sentence" is have same length. so, i changed equal condition of second if statement.

0

try using .replace

for example:

var str = "sentence."

str = str.replace(".", "")

same .replace logic can be used to loop through the input string and potentially add other characters such as /,; etc.

Moksha
  • 31
  • 1