我對JavaScript仍然陌生,所以請耐心等待......我有一系列的句子......每個單詞需要分割成一個數組,每個單詞的長度轉換爲數值,比較值的句子中的其他詞的數值來確定人物的拉爾數量,應返回數比較JavaScript中動態表中字符串的長度值
到目前爲止,我有:
function findLongestWord(str) {
\t var a = str.split(" "); //array for each word in str
\t var b = a.length - 1; //number of cells in array a
\t var c = 0; //counter for number of itterations
\t var d = []; //array to hold the numberic length of each word per cell
\t var e ; //compares cells and returns highest numberic value
var f = []; //just in case it is needed
for (a ; c < b ; c++) { //while c is less than b run code and add 1 to c
\t \t d[c].push(a[c].length) ; //should push the value of the length of a[c] into d[]
\t }
for (c = 0 ; d[c] < d.length ; c++) {
e = [d[c]].Math.max();//should return the larges value in d[]
}
return e;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
例如,在上面的句子中,最長的單詞是'jumped',並且應該返回6的值......我一直在努力研究這個小時並試圖找到正確的代碼......在某一時刻,代碼返回了' 1','3'或'19',其中'19'通過了其中一個句子,但沒有通過其他語句...現在,我要麼獲得空白輸出或var.push()未定義....
可能的重複:http://stackoverflow.com/questions/17386774/javascript-find-longest-word-in-a-string – jrook