저장을 습관화
프로그래머스 LV.0 배열 원소의 길이 본문
프로그래머스 LV.0 배열 원소의 길이
https://school.programmers.co.kr/learn/courses/30/lessons/120854
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 문제 명
배열 원소의 길이
2. 문제 설명
문자열 배열 strlist가 매개변수로 주어집니다.
strlist 각 원소의 길이를 담은 배열을 retrun하도록 solution 함수를 완성해주세요.
3. 제한 사항
1 ≤ strlist 원소의 길이 ≤ 100
strlist는 알파벳 소문자, 대문자, 특수문자로 구성되어 있습니다.
4. 예시
strlist | result |
["We", "are", "the", "world!"] | [2, 3, 3, 6] |
["I", "Love", "Programmers."] | [1, 4, 12] |
5. 기본 제공 코드
function solution(strlist) {
var answer = [];
return answer;
}
6. 제출한 내 답
const solution = (strlist) => {
let answer = [];
for (i = 0; i < strlist.length; i++) {
answer.push(strlist[i].length);
}
return answer;
};
6-2. VSC에 작성한 내용
const solution = (strlist) => {
let answer = [];
for (i = 0; i < strlist.length; i++) {
// let tmp = strlist[i].length;
// answer.push(tmp);
answer.push(strlist[i].length);
}
return answer;
};
// 어떻게 할 것인가?
// 배열의 각 항목을 불러다 글자수를 세어서
// 바로 배열 answer에 push
// 테스트
console.log(solution(["We", "are", "the", "world!"]));
// solution(["We", "are", "the", "world!"]);
console.log(solution(["I", "Love", "Programmers."]));
7. 특이사항
어렵지는 않았는데
나도 언제까지 for 문 쓰는것 보다
map이나 reduce 같은 배열 문법을 더 능숙하게 쓸수 있도록 해야겠다고 생각함
8. 다른 사람이 작성한 답
8-1.
function solution(strlist) {
return strlist.map((el) => el.length)
}
8-2.
function solution(strlist) {
return strlist.reduce((a, b) => [...a, b.length], [])
}
8-3.
function solution(strlist) {
var answer = [];
strlist.forEach(el=>answer.push(el.length))
return answer;
}
'코딩 테스트 > 프로그래머스 - 자바스크립트' 카테고리의 다른 글
프로그래머스 LV.0 덧셈식 출력하기 (0) | 2023.09.08 |
---|---|
프로그래머스 LV.0 대소문자 바꿔서 출력하기 (0) | 2023.09.07 |
프로그래머스 LV.0 문자열 반복해서 출력하기 (0) | 2023.09.07 |
프로그래머스 LV.0 a와 b 출력하기 (0) | 2023.09.07 |
프로그래머스 LV.0 문자열 출력하기 (0) | 2023.09.07 |