简体   繁体   English

在数组中找到第三大的单词 - CoderByte - javascript

[英]find third largest word in array - CoderByte - javascript

I am new to JS and am trying out CoderByte.我是 JS 新手,正在尝试 CoderByte。 The problem below I have gone around in circles trying to figure it out.下面的问题我绕着圈子试图弄清楚。 I am stuck on how to get to the third largest word.我被困在如何获得第三大词上。 Thanks in advance for the guidance.预先感谢您的指导。

Problem from CoderByte来自CoderByte 的问题

Using the JavaScript language, have the function ThirdGreatest(strArr) take the array of strings stored in strArr and return the third largest word within in. So for example: if strArr is ["hello", "world", "before", "all"] your output should be world because "before" is 6 letters long, and "hello" and "world" are both 5, but the output should be world because it appeared as the last 5 letter word in the array.使用 JavaScript 语言,让函数 ThirdGreatest(strArr) 获取存储在 strArr 中的字符串数组并返回 in 中的第三大单词。例如:如果 strArr 是 ["hello", "world", "before", " all"] 你的输出应该是 world 因为 "before" 是 6 个字母长,而 "hello" 和 "world" 都是 5,但输出应该是 world 因为它出现在数组中的最后 5 个字母单词。 If strArr was ["hello", "world", "after", "all"] the output should be after because the first three words are all 5 letters long, so return the last one.如果 strArr 是 ["hello", "world", "after", "all"] 输出应该是 after 因为前三个单词都是 5 个字母长,所以返回最后一个。 The array will have at least three strings and each string will only contain letters.该数组将至少包含三个字符串,每个字符串仅包含字母。

var arr = ["hello", "world", "before", "all"]; => world   sorted = ["all", "hello", "world", "before"]
var arr2 = ["hello", "world", "after", "all"]; => after   sorted = ["all", "hello", "world", "after"]
var arr3 = ['all', 'mary', 'jenn', 'frank', 'marshall', 'bob', 'sam', 'may']; => sorted = ["marshall", "frank", "mary", "jenn", "all", "bob", "sam", "may"]

var thirdGreatest = function (arr) {
    var arr2 = arr.sort(function(a,b){ return b.length - a.length});
    return arr2;
};

You are almost there, one sorted, just return the third item from the array:你快到了,一个排序,只需从数组中返回第三项:

var thirdGreatest = function (arr) {
    var arr2 = arr.sort(function(a,b){ return b.length - a.length});
    return arr2[2];
};

Remembering that arrays are zero indexed!记住数组是零索引的!

Sorting is expensive, and it might change the order of words of equal length.排序代价高昂,而且可能会改变等长单词的顺序。 Additionally, we're only interested in the length of words, not in their alphabetical order.此外,我们只对单词的长度感兴趣,而不对它们的字母顺序感兴趣。

This piece of code returns the correct results on every case given at CoderByte:这段代码在 CoderByte 给出的每个案例上返回正确的结果:

function ThirdGreatest(strArr) { 
  longest = '';
  second = '';
  third = '';
  for (idx in strArr) {
    current = strArr[idx];
    if (current.length > longest.length) {
      third = second;
      second = longest;
      longest = current;
    } else if (current.length > second.length) {
      third = second;
      second = current;
    } else if (current.length >= third.length) {
      third = current;
    }
  }
  return third;          
}

It keeps the longest three words seen so far, and if it encounters a word longer than any of the three, it pushes the shorter ones out.它保留迄今为止看到的最长的三个单词,如果遇到比三个单词中任何一个都长的单词,它会将较短的单词推出。

In the final else if statement, making ">" into >=" ensures that the third longest string will always be the latest string in the array if there are multiple with the same length在最后的 else if 语句中,将 ">" 变成 >=" 确保如果有多个相同长度的字符串,则第三长的字符串将始终是数组中的最新字符串

Why not just sort it then get the third item of the array?为什么不直接排序然后获取数组的第三项?

var thirdGreatest = function (arr) {
    // sorts the array
    var arr2 = arr.sort();

    // gets the third item (third largest)
    return arr2[2];
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM