如何修复数组中对象的方法和函数

时间:2017-09-19 11:45:15

标签: javascript arrays function object methods

我是Javascript的新手,这是我第一次在StackOverflow上发帖。大约一个星期左右我一直遇到问题,并且想知道这里是否有人可以提供帮助。我们应该创建一个阵列和分类的分类方法。 2个功能。我一直坚持这些:

  • hasMoreOscarsThan
    此方法接受一个actor对象作为参数,如果actor的Oscars多于作为参数传递的Oscars,则返回true,否则返回false。

  • getAverageAge 此函数返回数组中所有actor的平均年龄。

宣布主要功能& ARRAY

function famousPerson(name, age, oscars) {
    this.name = name;
    this.age = age;
    this.oscars = oscars;
    this.hello = function () {
        console.log("Hello, my name is " + this.name + ".");
    };
    this.hasMoreOscarsThan = function (x) {
        if ( == famousPerson.oscars && this.oscars > famousPerson.oscars) {
            return this.name;
        } else {
            return "False!";
        }
    };
};

在阵列中宣布对象

var actors = [];
actors[0] = new famousPerson("Leonardo DiCaprio", 41, 1);
actors[1] = new famousPerson("Jennifer Lawrence", 25, 1);
actors[2] = new famousPerson("Samuel L. Jackson", 67, 0);
actors[3] = new famousPerson("Meryl Streep", 66, 3);
actors[4] = new famousPerson("John Cho", 43, 0);

这些是方法&我写的函数:

// hasMoreOscarsThan method
actors.forEach(function (name.famousPerson); {
    // I put compare argument 2 and print result to console
    console.log(name.famousPerson.hasMoreOscarsThan(famousPerson.name));
});

// FUNCTIONS TO CALL
// getAverageAge function
var getAverageAge = (
    actors[0].age + actors[1].age + actors[2].age + actors[3].age + actors[4].age
) / actors.length;

对于hasMoreOscars,输入应该是另一个着名角色的名称。我能够测试一个像2这样的整数的oscars数量,但这不是问题所在......

getAverageAge应该是一个函数。我之前已将总和直接记录到控制台,但应该有更有效的方法。

非常感谢大家!

2 个答案:

答案 0 :(得分:0)

<强> hasMoreOscarsThan

您将x传递给函数但未实际使用它。见下文我为其他人更改了x ...并且根据问题应该返回true,而不是演员姓名。

function famousPerson(name, age, oscars) {
    this.name = name;
    this.age = age;
    this.oscars = oscars;
    this.hello = function() {
        console.log("Hello, my name is " + this.name + ".");
    };
    this.hasMoreOscarsThan = function(otherPerson) {
        if (this.oscars > otherPerson.oscars) {
            return true;
        } else {
            return false;
        }
    };
};

平均年龄

这是一个获得演员阵列平均年龄的函数。你可以看到它是如何工作的,首先它使用map创建一个actor年龄的数组,然后我使用一个名为sumarr的函数,我在下面也定义了这个函数,它给你一个数组的总和,然后显然除以其长度得到平均值。

function av_age(actors){
    var ages = actors.map(function(el){return el.age})
    var average = sumarr(ages)/actors.length
    return average
}

function sumarr(arr){
    return arr.reduce(function(tot, el){return tot+el});
}

或(全部在一个功能中)

function getAverageAge(actors){
    var sumOfAges= actors.reduce(function(tot, el){return tot+el.age},0)
    var average=sumOfAges / actors.length
    return average
}

答案 1 :(得分:0)

// It is good pracitece to use capital letter on constructor functions to remind users of your code 
// to use the NEW operator when creating instances of the object.
function FamousPerson(name, age, oscars) {
this.name = name;
this.age = age;
this.oscars = oscars;
this.hello = function() {
console.log("Hello, my name is " + this.name + ".");
};
this.hasMoreOscarsThan = function(otherFamousPerson) {
  // you pass a FamousPerson object as a parameter (named otherFamousPerson) and then
  // compare the oscar property of the otherFamousPerson parameter against the oscar property of this object.
  if (this.oscars > otherFamousPerson.oscars) {
      return this.name;
  } else {
      return "False!";
  }
};

};

// DECLARING OBJECTS IN ARRAY

var actors = [];
actors[0] = new FamousPerson("Leonardo DiCaprio", 41, 1);
actors[1] = new FamousPerson("Jennifer Lawrence", 25, 1);
actors[2] = new FamousPerson("Samuel L. Jackson", 67, 0);
actors[3] = new FamousPerson("Meryl Streep", 66, 3);
actors[4] = new FamousPerson("John Cho", 43, 0);

// Compare the oscars of 2 FamousPerson objects:
console.log(actors[3].hasMoreOscarsThan(actors[1]));

// Compute the average age of all FamousPerson objects in the actors array:
averageAge = actors.reduce(function(sum,actor){
  return sum+actor.age;
},0)/actors.length;

console.log(averageAge);

您可以在MDN上找到有关Array.prototype.reduce方法的更多信息:   https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce?v=a