본문 바로가기
코플릿 기록/JavaScript

고차함수 22번

by 뭉지야 2023. 1. 15.
728x90

#문제

영화 정보가 담긴 객체를 요소로 갖는 배열과 연도를 입력받아 해당 연도 이전의 영화를 요소로 갖는 배열을 리턴해야 합니다.

영화제목 by 영화감독 의 형태를 가져야한다.


#예시

let output = classicMovies(
  [
    {
      title: 'Batman',
      year: 1989,
      director: 'Tim Burton',
      imdbRating: 7.6,
    },
    {
      title: 'Batman Returns',
      year: 1992,
      director: 'Tim Burton',
      imdbRating: 7.0,
    },
    {
      title: 'Batman Forever',
      year: 1995,
      director: 'Joel Schumacher',
      imdbRating: 5.4,
    },
  ],
  1993
);

console.log(output); // --> ["Batman by Tim Burton", "Batman Returns by Tim Burton"]

#풀이

 

`${num.title} + by + ${num.director}` 이렇게 + 넣으면 안된다!


#정답

function classicMovies(arr, year) {
  //배열안에 객체 집단있다
  //filter로 year이전의 영화 걸러내고
  //map으로 title+by+director 하면 되겟다

  let oldlist = arr.filter(num => num.year < year);

  return oldlist.map(num => `${num.title} by ${num.director}`);
  }
728x90

'코플릿 기록 > JavaScript' 카테고리의 다른 글

고차함수 26번  (0) 2023.01.15
고차함수 24번  (0) 2023.01.15
고차함수 21번  (0) 2023.01.15
고차함수 20번  (0) 2023.01.15
고차함수 18번  (0) 2023.01.15