2017-06-20 105 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))