返回句子中最大单词的长度

Kōd*_*-ka 20 javascript

我写了一个函数,它接收一个句子并计算该句子中最长的单词.

function findLongestWord(str) {

  var charArray = str.split(" ");
  var wordArray = [];


  for(var i = 0; i < charArray.length; i++ ) {
    wordArray.push(charArray[i].length);
    wordArray.sort();
    wordArray.reverse();

  }

  return wordArray[0];
}
Run Code Online (Sandbox Code Playgroud)

我的功能适用于以下输入:

findLongestWord("The quick brown fox jumped over the lazy dog");
Run Code Online (Sandbox Code Playgroud)

但是当我通过它时:

findLongestWord("What if we try a super-long word such as otorhinolaryngology")
Run Code Online (Sandbox Code Playgroud)

该函数返回:

4
Run Code Online (Sandbox Code Playgroud)

而不是

19
Run Code Online (Sandbox Code Playgroud)

ade*_*neo 26

你的排序函数以词法方式对数组进行排序,因此你最终得到了

[1,10,19,2,2,2,3,4,4,4]
Run Code Online (Sandbox Code Playgroud)

扭转这一点,你得到

[4,4,4,3,2,2,2,19,10,1]
Run Code Online (Sandbox Code Playgroud)

4第一个数字在哪里

你不需要排序在所有的,只是使用Math.max替代

function findLongestWord(str) {
    return Math.max.apply( null, str.split(" ").map( (x) => x.length) );
}
Run Code Online (Sandbox Code Playgroud)


Mat*_*ari 25

TL; DR

您的号码将按字符串排序.

要将它们按数字排序,按降序排列,在函数中应该执行以下操作:

wordArray.sort(function(a, b) { return b - a; });
Run Code Online (Sandbox Code Playgroud)

说明

根据文件:

sort()方法对数组中的元素进行排序并返回数组.排序不一定稳定.默认排序顺序是根据字符串Unicode代码点.

[...]

句法

arr.sort()

arr.sort(compareFunction)

参数

compareFunction 可选的

指定定义排序顺序的函数.如果省略,则根据每个元素的字符串转换,根据每个字符的Unicode代码点值对数组进行排序.

强调我的,所以你最终得到这样的东西:

var str = "What if we try a super-long word such as otorhinolaryngology";
var charArray = str.split(" ");
// now charArray == ["What", "if", "we", "try", "a", "super-long", "word", "such", "as", "otorhinolaryngology"]
// when you take the length of each word and end up with
var wordArray = [4, 2, 2, 3, 1, 10, 4, 4, 2, 19];
// and if you use plain wordArray.sort() without specific sort function you get
wordArray = [1, 10, 19, 2, 2, 2, 3, 4, 4, 4];
// and once reversed it is
wordArray = [4, 4, 4, 3, 2, 2, 2, 19, 10, 1];
// this is why you end up with wordArray[0] == 4
Run Code Online (Sandbox Code Playgroud)

您还可以将整个功能实现为单行:

function findLongestWord(str) {
  return str.split(/\s+/).sort(function(a, b) { return b.length - a.length; })[0].length;
}

console.log("Longest word length = ", findLongestWord("The default sort order is according to string Unicode code points"));

console.log("Longest word length = ", findLongestWord("What if we try a super-long word such as otorhinolaryngology"));
Run Code Online (Sandbox Code Playgroud)


Ann*_*ine 15

与您的代码略有不同,但这应该有相同的结果!

function longestWord(string) {
    var str = string.split(" ");
    var longest = 0;
    for (var i = 0; i < str.length; i++) {
        if (longest < str[i].length) {
            longest = str[i].length;
        }
    }
    return longest;
}

console.log(longestWord("The quick brown fox jumped over the lazy dog"));
console.log(longestWord("What if we try a super-long word such as otorhinolaryngology"));
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的确定!这是正确的方法 - 没有构建愚蠢的"长度数组". (4认同)

Raj*_*esh 6

您可以获得一系列单词然后使用 Math.max.apply

function findLongestWord(str){
  return Math.max.apply(null, str.split(" ").map(x=>x.length));
}

var l = findLongestWord("What if we try a super-long word such as otorhinolaryngology")

console.log(l)
Run Code Online (Sandbox Code Playgroud)