以下方法和函數不工作,有人可以幫助我嗎?如何使方法和功能工作 - 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);
非常感謝你提前!
變化'的console.log(actors.firstName + 「」 + actors.lastName);''到的console.log(person.firstName + 「」 + person.lastName);'。點擊這裏 - https://fiddle.jshell.net/ermakovnikolay/rdyL6uqd/ –
格式化提示:將代碼粘貼到https://jsfiddle.net/,點擊「設置」,取消選中「縮進標籤」,選擇「縮進大小:4個空格「。確保第一行從第一列開始,即根本沒有縮進。然後點擊「整潔」,複製代碼並粘貼到帖子中。然後選擇代碼,然後點擊編輯器工具上的「{}」按鈕,或者按下鍵盤上的CTRL + K。這樣,在帖子上的代碼格式將永遠是正確的。 – Teemu