数组作为函数参数

时间:2017-02-28 14:21:16

标签: javascript

此代码段使用一系列名称。最后一部分应返回数组中的最大名称(按长度)。它返回0 .. 我怀疑它与传递数组"名称"作为功​​能参数.. 感谢。



var names = ["John", "Melissa", "Bob", "That guy with strange name"];
var j = 0;

function test(name) {
  return console.log(name + " name's length is " + name.length);
}

for (var i = 0; i < names.length; i++) {
  test(names[i]);
  if (names[i].length > 5) {
    console.log("here is name that has  more than 5 characters in it! -" + names[i] + '\n');
    j++;
  }
}

console.log("Amount of names that are bigger than 5 char. in array is " + j + '\n');

function findmax(array) {
  var max = 0;
  var a = array.length;
  for (var counter = 0; counter < a; counter++) {
    if (array[counter].length > max.length) {
      max = array[counter];
    }
  }
  return console.log("the largest name is " + max);
}

findmax(names);
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:1)

findmax函数中,您的条件是

if (array[counter].length > max.length)

您的最大值初始化为0,这是一个数字,因此number.lengthundefined

现在将任何值与undefined进行比较都会返回false。所以你的最大值永远不会改变。

尝试将其初始化为""

var names = ["John", "Melissa", "Bob", "That guy with strange name"];
var j = 0;

function test(name) {
  return console.log(name + " name's length is " + name.length);
}

for (var i = 0; i < names.length; i++) {
  test(names[i]);
  if (names[i].length > 5) {
    console.log("here is name that has  more than 5 characters in it! -" + names[i] + '\n');
    j++;
  }
}

console.log("Amount of names that are bigger than 5 char. in array is " + j + '\n');

function findmax(array) {
  var max = "";
  var a = array.length;
  for (var counter = 0; counter < a; counter++) {
    if (array[counter].length > max.length) {
      max = array[counter];
    }
  }
  return console.log("the largest name is " + max);
}

findmax(names);

答案 1 :(得分:1)

我只是想知道,"That guy with strange name"是一个名字?

var names = ["John", "Melissa", "Bob"];

function findmax(arr) {
  var max = 0;
  var word = '';
  names.forEach(function(v){
    if (v.length > max) {
      max = v.length;
      word = v;
    }
  });
  console.log(`The longest element is '${word}' with ${max} letters.`);
}

findmax(names);

答案 2 :(得分:0)

使用var max = 0;更改var max = "";,因为您正在使用max.length进行比较,希望max为字符串,而不是数字。

答案 3 :(得分:0)

只需将max=0更改为max='',因为长度属性仅适用于字符串,而不适用于数字。

max=0; //max.length -> undefined
max=''; // max.lenght -> 0

var names = ["John", "Melissa", "Bob", "That guy with strange name"];
var j = 0;

function test(name) {
    return console.log(name + " name's length is " + name.length);
}

for (var i = 0; i < names.length; i++) {
    test(names[i]);
    if (names[i].length > 5) {
        console.log("here is name that has  more than 5 characters in it! -" + names[i] + '\n');
        j++;
    }
}

console.log("Amount of names that are bigger than 5 char. in array is " + j + '\n');

function findmax(array) {
    var max = '';
    var a = array.length;
    for (var counter = 0; counter < a; counter++) {
        if (array[counter].length > max.length) {
            max = array[counter];
        }
    }
    return console.log("the largest name is " + max);
}

findmax(names);

答案 4 :(得分:0)

您不应该返回console.log。相反,您应该返回最大的名称并将文本放在console.log中。

function findmax(array) {
//code
return max;
};

console.log("the largest name is " + findmax(names));