Problem Solution/Programmers

[프로그래머스/JS] 배열 원소의 길이

yuri lee 2023. 9. 2. 16:39
반응형

Problem Description

문자열 배열 strlist가 매개변수로 주어집니다. strlist 각 원소의 길이를 담은 배열을 retrun하도록 solution 함수를 완성해주세요.

 

Restrictions

  • 1 ≤ strlist 원소의 길이 ≤ 100
  • strlist는 알파벳 소문자, 대문자, 특수문자로 구성되어 있습니다.

 

Input/Output Example

  • 입출력 예 #1 ["We", "are", "the", "world!"]의 각 원소의 길이인 [2, 3, 3, 6]을 return합니다.
  • 입출력 예 #2 ["I", "Love", "Programmers."]의 각 원소의 길이인 [1, 4, 12]을 return합니다.

 

My solution

function solution(strlist) {
    var result = []
    var step;
    for (step = 0; step < strlist.length; step++) {
        result.push(strlist[step].length)
    }
    return result
}

 

Another solutions

function solution(strlist) {
    return strlist.map((el) => el.length)
}
function solution(strlist) {
    return strlist.reduce((a, b) => [...a, b.length], [])
}
function solution(strlist) {
    var answer = [];
    strlist.forEach(el=>answer.push(el.length))
    return answer;
}

 


https://school.programmers.co.kr/learn/courses/30/lessons/120854

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/map

 

Array.prototype.map() - JavaScript | MDN

map() 메서드는 배열 내의 모든 요소 각각에 대하여 주어진 함수를 호출한 결과를 모아 새로운 배열을 반환합니다.

developer.mozilla.org

 

반응형