2016-09-28 3 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 + ")";'. 여기를 확인하십시오 - https://fiddle.jshell.net/ermakovnikolay/rdyL6uqd/ –

+0

서식 팁 : https://jsfiddle.net/에 코드를 붙여넣고 "설정"을 클릭하고 "탭 들여 쓰기"를 선택 해제하고 "들여 쓰기"를 선택하십시오 크기 : 4 칸 ". 첫 번째 줄이 열 1에서 시작해야합니다. 즉 들여 쓰기가 전혀 없습니다. 그런 다음 "깔끔한"을 클릭하고 코드를 복사 한 다음 게시물에 붙여 넣으십시오. 그런 다음 코드를 선택하고 편집기 도구에서'{}'버튼을 클릭하거나 키보드의 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); 

결과 :

안녕하세요, 제 이름은 안녕 내 이름은

메릴

입니다, 내 이름은 안녕

사무엘 L.이다, 내 이름은 제니퍼

안녕하세요입니다, 안녕하세요

레오나르도

입니다 , 제 이름은 John입니다

거짓! 거짓!
거짓!
Meryl
거짓! "; CONSOLE.LOG`에`(Person.firstName에 + (+ actors.lastName"+ person.lastName)
레오나르도 디카프리오 48.4

0

인수는 person 개체입니다하지만 당신은 함수 내에서 아무 곳이나 정의되지 않은 일부 actors 언급하는, 그래서 person.firstName & person.lastName

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

JSFIDDLE

관련 문제