2016-10-27 46 views
0

編寫一個名爲「getLengthOfShortestElement」的函數。找到數組中最短元素的第一個實例

給定一個數組,「getLengthOfShortestElement」返回給定數組中最短字符串的長度。

備註: *如果數組爲空,它應該返回0。

我的代碼:

function getLengthOfShortestElement(arr) { 

    if (arr.length === 0) return 0; 
     return arr.sort(function(a, b){ 
      return a.length> b.length; 
     }).unshift(); 
} 

getLengthOfShortestElement(['one', 'two', 'three']); // 3 

爲什麼不通過測試,它應該「處理關係」通過僅返回最短元素的第一個實例。另外,有沒有更好的方法使空數組返回0?

+2

你是什麼意思「爲什麼不通過測試,它應該‘處理關係’僅返回最短元素的第一個例子「。 ?它應該返回一個長度還是一個實例?目前還不清楚 –

+0

它返回給定數組中最短字符串的長度。如果多個元素共享相同的最短長度,它將返回僅第一個元素的長度。這是反饋我從REPL得到:對失敗的測試 it_should_handle_ties 更多信息 錯誤:預計3爲2 以n – sopstem2428

+0

*「?有沒有做一個空數組返回0的一種更好的方式」 * - 這有什麼錯你目前使用的簡單直接的技術?順便說一句,你的排序函數應該'返回a.length - b.length;'(比較器不應該返回一個布爾值)。 – nnnnnn

回答

0

由於JavaScript的Array.prototype.sort文檔clearly states

The sort() method sorts the elements of an array in place and returns the array. The sort is not necessarily stable.

換句話說,也不能保證具有相同長度的元素會在排序結果的順序相同,因爲他們在原來的。

也許更好的方法是繞過可能不穩定的排序並簡單地自己處理數組。這可以用下面的僞代碼很容易做到:

def getFirstShortestElement(array): 
    if len(array) == 0: 
     return 0 
    firstSmall = 0 
    for index = 1 to len(array) - 1, inclusive: 
     if len(array[index]) < len(array[firstSmall]): 
      firstSmall = index 
    return array[firstSmall] 

As an aside, your function is spectacularly misnamed. It does not give you the "LengthOfShortestElement" at all. Function names should reflect what they do, you'll find your code much easier to maintain if you follow that rule :-)

+0

那麼我應該如何解決這個問題呢? – sopstem2428

+0

@ Tyler.Borer,用*方法*更新,您可以使用它,這取決於您將其轉換爲您選擇的語言。 – paxdiablo

+0

謝謝。我正在按照提供的練習,這不是最差的錯誤名稱 – sopstem2428

0

它應該適用於所有情況的作品。

function getLengthOfShortestElement(arr){ 

    if (arr.length === 0) return 0; 

    var shortestLength = arr[0].length; 
    arr.forEach(function (each) { 
     shortestLength = each.length < shortestLength ? each.length : shortestLength; 
    }); 

    return shortestLength; 
} 
+0

*「如果數組爲空,它應該返回0」* – nnnnnn

+0

如果所有字符串都是10,000,000個字符以上,該怎麼辦? :-) – paxdiablo

1

這可以用減速機完成。

function getLengthOfShortestElement(arr) { 

    if (!arr.length) return 0; 

    return arr.reduce(function(prev, current) { 

     if (prev === null) return current.length; 
     if (current.length < prev) return current.length; 

     return prev; 
    }, null); 
} 
+0

'.reduce()'是一個很好的方法,但是你顯示的代碼不會返回* element *,它會返回元素的長度。 – nnnnnn

+0

@nnnnnn'如果(!arr.length)返回0;' – tom10271

+1

而不是以'null'開始,你可以簡單地從'Infinity'開始並放棄第一個條件。 – Bergi

0

我不明白你的意思是「爲什麼不通過測試,它應該‘處理關係’通過僅返回最短元素的第一個實例」。

但空數組返回0,我會做:

function getLengthOfShortestElement(arr){ 
    if (!arr){return 0} 
    //rest of your code 
} 
0

您使用不印字,其中前添加一個元素,而不是刪除它,你就錯過了length屬性。

function getLengthOfShortestElement(arr) { 
    if (!arr || !arr.length) {return 0;} 
    arr = [].concat(arr); // Prevent altering the source array 
    return arr.sort(function (a, b) { 
     return a.length > b.length; 
    }).shift().length; 
} 

getLengthOfShortestElement(['one', 'two', 'three']); // 3 

對於長的陣列,它可能更快,如果你旅行的陣列檢查長度:

function getLengthOfShortestElement(arr) { 
    if (!arr || !arr.length) {return 0;} 
    var minlength = (""+arr[0]).length; 
    for (var i = 1; i < arr.length; i++) { 
     var len = (""+arr[i]).length; 
     if (len < minlength) {minlength = len;} 
    } 
    return minlength; 
} 
相關問題