[TIL] 내배캠4기-React-46일차

hare·2022년 12월 15일
0

내배캠-TIL

목록 보기
34/75

오전/오후

리액트 숙련 개인과제 끝내기

  • TIL

프로그래머스 코테

문제 설명
문자열로 구성된 리스트 strings와, 정수 n이 주어졌을 때, 각 문자열의 인덱스 n번째 글자를 기준으로 오름차순 정렬하려 합니다. 예를 들어 strings가 ["sun", "bed", "car"]이고 n이 1이면 각 단어의 인덱스 1의 문자 "u", "e", "a"로 strings를 정렬합니다.

정답 제출 코드

function solution(strings, n) {
  //   const newArr = strings.map((item) => item.charAt(n)).sort(); //[ 'a', 'e', 'u' ]
  const newArr = strings
    .map((item) => {
      temp = item.substring(n, n + 1) + item;
      return temp;
    })
    .sort()
    .map((item) => item.slice(1));
  // 문자열을 붙였다가.. 잘라요..
  //   const result = [];
  //   result.push(strings.find((idx) => idx.newArr));
  return newArr;
}

막힌 부분

function solution(strings, n) {
 const newArr = strings.map((item) => item.charAt(n)).sort(); 
  //[ 'a', 'e', 'u' ]
  return newArr;
}
strings1 = ["sun", "bed", "car"];
strings2 = ["abce", "abcd", "cdx"];
const n1 = 1;
const n2 = 2;
console.log(solution(strings1, n1));
console.log(solution(strings2, n2));

n번째 인덱스 추출 후 정렬까진 ok
➡️ 그 후에 원래 문자열에 합치고 다시 정렬하는 방법이 떠오르지 않았다..
💡 팀원들과의 스터디를 통해

  • substring()
  • slice()
    를 써서 해결
    주말에 비슷한 문제들 따로 정리해봐야겠다
profile
해뜰날

0개의 댓글