본문 바로가기
👩‍💻 Programming/Coding Test 문제 풀이

[Programmers] level 1: 신규 아이디 추천 by JavaScript

by codingBear 2022. 6. 28.
728x90
반응형

 이번 글은 아래 링크의 글들을 참조하여 작성하였습니다.

https://tech.kakao.com/2021/01/25/2021-kakao-recruitment-round-1/

 

2021 카카오 신입공채 1차 온라인 코딩 테스트 for Tech developers 문제해설

지난 2020년 9월 12일 토요일 오후 2시부터 7시까지 5시간 동안 2021 카카오 신입 개발자 공채 1차 코딩 테스트가 진행되었습니다. 테스트에는 총 7개의 문제가 출제되었으며, 개발 언어는 C++, Java, Jav

tech.kakao.com

https://velog.io/@leeeunbin/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-%EC%8B%A0%EA%B7%9C-%EC%95%84%EC%9D%B4%EB%94%94-%EC%B6%94%EC%B2%9C-JavaScript

 

[프로그래머스] 신규 아이디 추천 - JavaScript

2021 KAKAO BLIND RECRUITMENT

velog.io


 이번 문제에 대한 자세한 사항은 아래 링크를 참조하길 바란다.

https://programmers.co.kr/learn/courses/30/lessons/72410

 

코딩테스트 연습 - 신규 아이디 추천

카카오에 입사한 신입 개발자 네오는 "카카오계정개발팀"에 배치되어, 카카오 서비스에 가입하는 유저들의 아이디를 생성하는 업무를 담당하게 되었습니다. "네오"에게 주어진 첫 업무는 새로

programmers.co.kr


 처음에는 정규표현식을 사용하지 않고 함수를 통해 하나하나 조건을 충족시키려고 했다. 허나 잘 구현되지 않았고 결국 다른 사람의 코드를 참고하였다. 정규표현식을 활용하면 간단하게 해결 가능한 문제였다.

 

Solutions

Solution 1.
function solution(new_id) {
  let answer = new_id
    .toLowerCase() // 1단계
    .replace(/[^\w-_.]/g, '') // 2단계
    .replace(/\.{2,}/g, '.') // 3단계
    .replace(/^\.|\.$/g, '') // 4단계
    .replace(/^$/g, 'a') // 5단계
    .slice(0, 15) // 6단계
    .replace(/\.$/g, '');

  if (answer.length <= 2) {
    answer = answer.padEnd(3, answer[answer.length - 1]);
  }

  return answer;
}

함께 보기

  • 정규 표현식 정리
    • \w: 문자 및 숫자 검색
    • []: [] 안의 값 검색
    • {n, }: n회 이상 반복되는 값 검색
    • |: 또는
    • ^: 말머리부터 검색
    • $: 말꼬리부터 검색
ex) ^: 말머리부터 검색
'This is a pen!'.match(/^This/);  // ["This"] 
'This is a pen!'.match(/^is/);  // null
ex) $: 말꼬리부터 검색
'This is a pen!'.match(/^This/);  // ["This"] 
'This is a pen!'.match(/^is/);  // null

 

  • padEnd() method

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd

 

String.prototype.padEnd() - JavaScript | MDN

The padEnd() method pads the current string with a given string (repeated, if needed) so that the resulting string reaches a given length. The padding is applied from the end of the current string.

developer.mozilla.org

  • padStart() method

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart

 

String.prototype.padStart() - JavaScript | MDN

The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string.

developer.mozilla.org

 

728x90
반응형

댓글