返回最大的数字 - JS

时间:2017-09-01 08:44:47

标签: javascript arrays

我在这里创建了一个需要的函数:

  1. 返回最大的号码'在阵列中。
  2. 如果数组为空则返回0
  3. 如果没有'号码,则返回0在阵列中。
  4. 如果数组中有一个,则返回最高的负数。
  5. 目前,它适用于负数和空数组,但由于某种原因,它不会显示最大数字。

    这是我的功能:

    
    
    function largestNumber(arr) {
    
      for (var i = 0; i < arr.length; i++) {
        if (typeof arr[i] === 'number') {
          var longestNumber = arr[i];
          if (arr[i] > longestNumber) {
            longestNumber = arr[i];
          }
        }
        return longestNumber;
      }
    
      if (arr.length === 0) {
        return 0;
      }
    
    }
    
    var output1 = largestNumber([3, "word", 5, "up", 3, 1]);
    console.log(output1); // --> must be 5 but display 3 (as the largest)
    
    var output2 = largestNumber([-1, -5, "word", -3]);
    console.log(output2); // --> -1
    
    var output3 = largestNumber([]);
    console.log(output3); // --> 0
    
    var output4 = largestNumber(["word", 3, 5, 3, "wordy", "up", 1, 5]);
    console.log(output4); // --> must be 5 instead returned undefined
    &#13;
    &#13;
    &#13;

    知道造成这种情况的原因是什么?如何改进我的代码以获得预期的结果?

5 个答案:

答案 0 :(得分:2)

只需过滤数组以仅保留数字,然后使用function largestNumber(arr) { let n = arr.some(e => e<0); arr = arr.filter(v => n ? !isNaN(v) && v<0 : !isNaN(v)); return arr.length === 0 ? 0 : Math.max(...arr); } console.log(largestNumber(["word", 3, 5, 3, "wordy", "up", 1, 5])); console.log(largestNumber([-1, -2])); console.log(largestNumber([3, "word", 5, "up", 3, 1])); console.log(largestNumber([-1, -5, "word", -3])); console.log(largestNumber([])); console.log(largestNumber(["word", 3, 5, 3, "wordy", "up", 1, 5])); console.log(largestNumber([-8, -4, 1, 5]));

&#13;
&#13;
public class EnviornmentControls
{
    public int Id { get; set; }
    ...

    public virtual Environment Environment { get; set; }
}
&#13;
&#13;
&#13;

答案 1 :(得分:2)

您的函数中的这两个连续行应该为您提供问题的线索:

for

您将当前号码设置为最长号码,然后将其与同一号码进行比较。

只需移动return循环外的第一行,并将其默认为零。您还必须将if移出循环,并稍微更改function largestNumber(arr) { var longestNumber = -Infinity; for (var i = 0; i < arr.length; i++) { if (typeof arr[i] === 'number') { if (arr[i] > longestNumber) { longestNumber = arr[i]; } } } if (longestNumber == -Infinity) { longestNumber = 0; } return longestNumber; } var output1 = largestNumber([3, "word", 5, "up", 3, 1]); console.log(output1); // --> must be 5 but display 3 (as the largest) var output2 = largestNumber([-1, -5, "word", -3]); console.log(output2); // --> -1 var output3 = largestNumber([]); console.log(output3); // --> 0 var output4 = largestNumber(["word", 3, 5, 3, "wordy", "up", 1, 5]); console.log(output4); // --> must be 5 instead returned undefined逻辑以满足您的所有要求

&#13;
&#13;
{{1}}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

   if(typeof arr[i] ===  'number'){
     var longestNumber = arr[i];
     if(arr[i] > longestNumber){
       longestNumber = arr[i];
     }

这里你在循环的每次执行中设置var longestNumber = arr[i],这意味着函数最后会返回最后一个数字元素。相反,这样做:

function largestNumber(arr) {
    var longestNumber = -Infinity;
    for (var i = 0; i < arr.length; i++) {
        if (typeof arr[i] === 'number' && arr[i] > longestNumber) {
            longestNumber = arr[i];
        }
    if (longestNumber > -Infinity) {
        return longestNumber;
    }
    return 0;
}

答案 3 :(得分:0)

我将@baao的代码从地址的回答改编为请求编号4

&#13;
&#13;
  function largestNumber(arr) {
    arr = arr.filter(v => !isNaN(v));
    negative = arr.filter(v => v < 0);
    return negative.length === 0 ? Math.max(0, ...arr) : Math.min(...arr)
  }

  console.log(largestNumber(["word", 3, 5, 3, "wordy", "up", 1, 5]));
  console.log(largestNumber([-1, -2]));
  console.log(largestNumber([3, "word", 5, "up", 3, 1]));
  console.log(largestNumber(["word", "up"]));
  console.log(largestNumber([-1, -5, "word", -3]));
  console.log(largestNumber([]));
  console.log(largestNumber(["word", 3, 5, 3, "wordy", "up", 1, 5]));
&#13;
&#13;
&#13;

答案 4 :(得分:0)

您可以循环数组并检查undefined或者值是否大于storte值。

&#13;
&#13;
function largestNumber(array) {
    return array.reduce(function (r, a) {
        return typeof a === 'number' && (r === undefined || r < a) ? a : r;
    }, undefined) || 0;
}

console.log(largestNumber([3, "word", 5, "up", 3, 1]));             //  5
console.log(largestNumber([-1, -5, "word", -3]));                   // -1
console.log(largestNumber([]));                                     //  0
console.log(largestNumber(["word", 3, 5, 3, "wordy", "up", 1, 5])); //  5
&#13;
&#13;
&#13;

使用for循环。

&#13;
&#13;
function largestNumber(array) {
    var i, longestNumber;
    for (var i = 0; i < array.length; i++) {
        if (
            typeof array[i] === 'number' &&
            (longestNumber === undefined || longestNumber < array[i])
        ) {
            longestNumber = array[i];
        }
    }
    return longestNumber || 0;
}

console.log(largestNumber([3, "word", 5, "up", 3, 1]));             //  5
console.log(largestNumber([-1, -5, "word", -3]));                   // -1
console.log(largestNumber([]));                                     //  0
console.log(largestNumber(["word", 3, 5, 3, "wordy", "up", 1, 5])); //  5
&#13;
&#13;
&#13;