簡體   English   中英

如何使方法和函數起作用-Javascript

[英]how to make methods and functions work - Javascript

以下方法和功能不起作用,有人可以幫助我嗎?

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);

提前非常感謝您!!!

參數是person對象,但是您引用的是一些在函數內部未定義的actors ,因此更改為person.firstNameperson.lastName

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

JSFIDDLE

我編輯了您的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM