codes/고차 함수

16_getLengthOfElements

Mia_ 2022. 11. 20. 22:34
function getLengthOfElements(arr) {
  //Q. 문자열을 요소로 갖는 배열을 입력받아 각 요소의 길이를 요소로 갖는 새로운 배열 리턴
  //주의! map() 메서드 사용!

  //getLengthOfElements(['hello', 'code', 'states']);
  //console.log(output); // --> [5, 4, 6]

  //1. 배열의 길이를 얻는 함수 만들기
  //2. map으로 요소마다 적용 시켜주기

  const getLength = function(el){
    return el.length;
  }

  return arr.map(getLength);

}
//다시 풀어본 코드
function getLengthOfElements(arr) {
  //Q. 문자열을 요소로 갖는 배열을 입력받아 각 요소의 길이를 요소로 갖는 새로운 배열 리턴
  //주의! map() 메서드 사용!

  //getLengthOfElements(['hello', 'code', 'states']);
  //console.log(output); // --> [5, 4, 6]

  return arr.map(function(el){
    return el.length;
  });
}