以下のメソッドと関数は動作しません。メソッドと関数を動作させる方法 - 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スペース "。最初の行が列1から始まっていること、つまりインデントが全くないことを確認してください。その後、「整頓」をクリックし、コードをコピーして投稿に貼り付けます。次に、コードを選択し、エディタツールの '{}'ボタンをクリックするか、キーボードのCTRL + Kを押します。そうすれば、投稿のコードの書式は常に正しいでしょう。 – Teemu