2016-09-28 61 views
0

以下方法和函數不工作,有人可以幫助我嗎?如何使方法和功能工作 - 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); 

非常感謝你提前!

+0

變化'的console.log(actors.firstName + 「」 + actors.lastName);''到的console.log(person.firstName + 「」 + person.lastName);'。點擊這裏 - https://fiddle.jshell.net/ermakovnikolay/rdyL6uqd/ –

+0

格式化提示:將代碼粘貼到https://jsfiddle.net/,點擊「設置」,取消選中「縮進標籤」,選擇「縮進大小:4個空格「。確保第一行從第一列開始,即根本沒有縮進。然後點擊「整潔」,複製代碼並粘貼到帖子中。然後選擇代碼,然後點擊編輯器工具上的「{}」按鈕,或者按下鍵盤上的CTRL + K。這樣,在帖子上的代碼格式將永遠是正確的。 – Teemu

回答

0

我編輯您的代碼:

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

因此, :

你好,我的名字是萊昂納多

你好,我的名字是珍妮弗

你好,我的名字叫塞繆爾·

你好,我的名字是梅麗爾

你好,我的名字是John

錯! 錯!
錯!
Meryl
錯!
萊昂納多 48.4

0

的說法是person對象,但你是指一些actors未在函數內部的任何地方定義,因此改爲person.firstName & person.lastName

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

JSFIDDLE