【问题标题】:how to make methods and functions work - Javascript如何使方法和函数工作 - Javascript
【发布时间】:2016-09-28 15:56:02
【问题描述】:

以下方法和功能不起作用,有人可以帮帮我吗?

hasMoreOscarsThan - this method accepts one actor object as a parameter and
    returns true if the actor has more Oscars than the one that is passed as
    a parameter and false otherwise.

现在编写以下函数:

getActorByName - this function expects a string as a parameter and returns
    the object in the actors array whose name property is equal to the
    string that is passed in (if there is one).

我的代码:

function Person(firstName, lastName, age, numOscars) {

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

};


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

actors.forEach(function(item) {
    item.hello();
})

actors.forEach(function(item) {
    item.hasMoreOscarsThan();
})


function getActorByName(person) {
    console.log(actors.firstName + " " + actors.lastName);
}


function list() {
    var actorsLength = actors.length;
    for (var i = 0; i < actorsLength; i++) {
        getActorByName(actors[i]);
    }
}


var search = function(lastName) {
    var actorsLength = actors.length;
    for (var i = 0; i < actorsLength; i++) {
        if (lastName == actors[i].lastName) {
            getActorByName(actors[i]);
        }
    }
}


search("DiCaprio");

var getAverageAge;

getAverageAge = (actors[0].age + actors[1].age + actors[2].age + actors[3].age + actors[4].age) / actors.length;
console.log(getAverageAge);

提前非常感谢!!!

【问题讨论】:

  • console.log(actors.firstName + " " + actors.lastName); 更改为console.log(person.firstName + " " + person.lastName);。在这里查看 - fiddle.jshell.net/ermakovnikolay/rdyL6uqd
  • 格式化提示:将代码粘贴到jsfiddle.net,点击“设置”,取消选中“使用制表符缩进”,选择“缩进大小:4 个空格”。确保第一行从第 1 列开始,即根本没有缩进。然后点击“整理”,复制代码,粘贴到帖子中。然后选择代码,然后单击编辑器工具上的{} 按钮,或按键盘上的 CTRL+K。这样,帖子上的代码格式将始终正确。

标签: javascript arrays function object methods


【解决方案1】:

我编辑了你的代码:

function Person(firstName, lastName, age, numOscars) {

    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.numOscars = numOscars;
    this.hello = function() { console.log("Hello, my name is " + this.firstName); }
    this.hasMoreOscarsThan = function(x) { // x is a compare parameter
        if (this.numOscars > x) { // comparing numOscars with argument x
            return this.firstName;
        } else {
            return "False!";
        }
    }

};


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

actors.forEach(function(item) {
    item.hello();
})

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


function getActorByName(person) {
    console.log(person.firstName + " " + person.lastName); // changed actors to person
}


function list() {
    var actorsLength = actors.length;
    for (var i = 0; i < actorsLength; i++) {
        getActorByName(actors[i]);
    }
}


var search = function(lastName) {
    var actorsLength = actors.length;
    for (var i = 0; i < actorsLength; i++) {
        if (lastName == actors[i].lastName) {
            getActorByName(actors[i]);
        }
    }
}


search("DiCaprio");

var getAverageAge;

getAverageAge = (actors[0].age + actors[1].age + actors[2].age + actors[3].age + actors[4].age) / actors.length;
console.log(getAverageAge);

结果:

你好,我叫莱昂纳多

你好,我叫詹妮弗

你好,我是塞缪尔 L。

你好,我叫梅丽尔

你好,我叫约翰

错误! 假的!
假的!
梅丽尔
假的!
莱昂纳多·迪卡普里奥 48.4

【讨论】:

    【解决方案2】:

    参数是personobject,但你指的是一些actors,它没有在函数内部的任何地方定义,所以改为person.firstName&person.lastName

    function getActorByName(person) {
        console.log(person.firstName + " " + person.lastName);
    }
    

    JSFIDDLE

    【讨论】:

      猜你喜欢
      • 2017-02-04
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      相关资源
      最近更新 更多