2017-06-20 108 views
0

我正在试图创建一个函数来告诉我句子中最长的单词有多长。我的方法是将句子拆分成单词串。我现在有一串字符串。我的问题是我想用这个数组来获得另一个数字数组,即每个单词的长度。我该怎么做呢?我的代码如下,但我一直得到空。从一个句子中获取长度与单词长度的数组-javascript

function findLongestWord(str) { 
    var split = str.split(" "); 
    for (j = 0; j < split.length; j++) 
    var wordCount = split[j].length; 
    var lengths = []; 
    for (var i = 0; i < wordCount.length; i++) { 
    lengths.push(i); 
    } 

    return Math.max(...lengths); 
} 
+1

如果要打开一个列表/阵列到另一个(要打开单词列表一定长度的列表),并且将所得列表是相同的长度与原始列表中,使用'map'函数。 'map'接受一个列表,对每个元素应用一个函数,然后返回结果列表。 – Carcigenicate

+0

如果它是一个句子,它可能包含标点符号。那么你的分裂(“”)是不会工作的。我认为你的问题是要找出单词列表中最长单词的长度。你应该总是尽量避免使用关键字/函数名称作为你的变量名称,如分割。 –

回答

-1

您需要使用var在第一个for循环中创建j,就像您为第二个for循环所做的那样。

0

如果您要遍历所有单词,则可以在输入数组中找到最长(最长)的单词。

function findLongestWord(str) { 
    var split = str.split(" "); 
    var maxLength = 0; 
    var longestWord = ""; // If no word is found "".length will return 0 
    var len = split.length; 
    for (j = 0; j < len; j++) 
    { 
    if (split[j].length > maxLength) 
    { 
     longestWord = split[j]; 
     maxLength = split[j].length; 
    } 
    } 

    return longestWord; 
} 

和返回值.length得到的长度(或return maxLength如果你愿意的话)。

注意取决于您的应用程序标点符号可能会干扰您的算法。

-1

这可以使用.map()方法完成。您的字符串数组映射到字长的数组,然后返回长度的阵列Math.max(),像这样:

function findLongestWord(str) { 
 
    // map words into array of each word's length, grab highest # 
 
    return Math.max(...str.split(" ").map(str => str.length)); 
 
} 
 

 
console.log(findLongestWord("The quick brown fox jumped over the lazy dog"));

0

我做了有关的错误一些意见你的代码

function findLongestWord(str) { 
 

 
    // better use .split(/\s+/) instead to remove trailing space in the middle of sentence 
 
    var split = str.split(" "); 
 
    
 
    // this for loop is redundant, you have to wrap the code that you want to loop with curly brackets. 
 
    for (j = 0; j < split.length; j++) 
 
    
 
    // the value of j would be the length of split array. 
 
    var wordCount = split[j].length; 
 
    var lengths = []; 
 
    
 
    // since wordCount.length is undefined, so loop never gets excuted and your lengths array would be empty. 
 
    for (var i = 0; i < wordCount.length; i++) { 
 
    lengths.push(i); 
 
    } 
 
    
 
    // doing Math.max on empty array will return -Infinity 
 
    return Math.max(...lengths); 
 
} 
 

 
findLongestWord('hello there mate')

下面是我的解决方案。还有更多的方式来做你想做的事情。

function findLongestWord(str) { 
 
    // trim trailing white space. 
 
    var split = str.trim().split(/\s+/); 
 
    var lengths = []; 
 
    
 
    // loop through array of words 
 
    for (j = 0; j < split.length; j++) { 
 
    
 
    // check the length of current words 
 
    var wordCount = split[j].length; 
 
    lengths.push(wordCount); 
 
    } 
 
    
 
    return Math.max(...lengths); 
 
} 
 

 
const sentence = 'hello its a me mariooooooo'; 
 

 
console.log(findLongestWord(sentence)) 
 

 
// one liner - using reduce function 
 
const findLongestWord2 = (str) => str.trim().split(/\s+/).reduce((a, b) => a.length > b.length ? a.length : b.length, -Infinity); 
 

 
console.log(findLongestWord2(sentence)) 
 

 
// less efficient but shorter - using sort 
 
const findLongestWord3 = (str) => str.trim().split(/\s+/).sort((a, b) => a.length - b.length).pop().length; 
 

 
console.log(findLongestWord3(sentence))