분류 전체보기
-
Javascript - HoistingJavascript 2023. 5. 16. 18:01
얼마 전 면접을 본적이 있는데 js에서의 호이스팅에 대해 다른 면접자에게 질문을 하는 것을 보았다. 얼추 어떤 개념인지는 머릿속으로는 알고 있었지만 막상 나도 설명을 해보려고 생각해보니까 정확히 알고 있지 않은 것 같아 정리를 하게 되었다. Hoisting 이란? 함수 안에 있는 선언들을 모두 끌어올려서 해당 함수 유효 범위의 최상단에 선언하는 것 자바스크립트 함수는 실행되기 전에 함수 안에 필요한 변수값들을 모두 모아서 유효 범위의 최상단에 선언한다. 자바스크립트 Parser가 함수 실행 전 해당 함수를 한 번 훑는다. 함수 안에 존재하는 변수/함수선언에 대한 정보를 기억하고 있다가 실행시킨다. 유효 범위: 함수 블록 {} 안에서 유효 즉, 함수 내에서 아래쪽에 존재하는 내용 중 필요한 값들을 끌어올리..
-
프로그래머스 - 신규 아이디 추천Algorithm 2023. 5. 15. 19:16
문제 풀이 주어진 문제조건에서 단계별로 친절하게 설명되어 있어 단계별로 진행하였다. 1단계 : new_id 문자열을 소문자로 변환 2단계 : '-', '.', '_'을 제외한 모든 특수문자을 찾아내는 정규식으로 replaceAll을 이용해 특수문자를 제거 3단계 : 문자열에 점이 두번 연속 존재하는 ".." 문자열이 존재하지 않을 때 까지 문자열에서 ".."문자열을 "."로 변환 4단계 : 문자열의 첫 글자나 마지막 글자가 "." 특수문자일 경우 마지막 글자 제거 5단계 : 문자열 길이가 0이라면 문자열에 "a" 추가 6단계 : 문자열의 길이가 15글자를 넘는다면 15글자까지 잘라내고 마지막 글자가 "." 특수기호일 경우 마지막 글자 제거 7단계 : 문자열의 길이가 3보다 작다면 문자열의 길이가 3 이상..
-
프로그래머스 - 과일 장수Algorithm 2023. 5. 12. 16:00
문제 풀이 처음 풀고자 했던 의도는 score 배열을 내림차순으로 정렬한 뒤, 0번째 인덱스부터 m개씩 만큼 뽑아 최솟값을 구한뒤 m을 곱한 값들을 다 더하면 될 거라고 생각했다 function solution(k, m, score) { let answer = 0; let slicedScore = []; // 내림차순 정렬 score.sort((a, b) => b - a); for(let i = 0; i m-1)) { // score 배열의 i번째 인덱스 값부터 i+m-1번째 인덱스 값까지 뽑아 ..
-
프로그래머스 - 신고 결과 받기Algorithm 2023. 5. 12. 14:53
문제 풀이 먼저 한 유저가 같은 유저를 여러번 신고를 해도 1회 신고로 처리하기 위해 report 배열에서 중복제거를 해준다. 이 후 id_list와 매칭할 수 있는 유저당 신고 당한횟수 배열과 유저당 받은 메일 횟수 배열을 만들어 유저당 신고 당한횟수 배열에 유저당 신고당한 횟수를 채운다. 이 후 신고당한 횟수가 정지 조건인 k보다 많은 인덱스에 해당하는 유저당 받은 메일 횟수 배열 인덱스를 증가시킨다. 코드를 보면서 예시 1번을 진행해보자. function solution(id_list, report, k) { // report 배열에서 중복 제거 let setReportArray = [...new Set(report)]; // 각 유저당 신고당한 횟수 이 배열은 id_list 유저 순서와 동일 ex..
-
useRef current 객체의 생성 시점에 따른 ref 접근방식Typescript 2023. 5. 8. 18:26
react Js로 작성한 인스타그램 클론코딩을 typescript로 migrate 하는 작업을 하던 중 생각지도 못한 자주 발생한 에러들이 있었다. 바로 useRef를 사용해 컴포넌트에 직접 접근하려고 할 때 발생한 이 오류이다. 처음에는 초기값을 지정해 주지 않아 발생한 오류라고 생각해 수정했었으나.. 단순히 useRef 초기값을 null로 지정해주면 해결될 문제인줄 알았더니 그것 또한 아니었다 미리 스포하자면 inputRef.current가 존재하면 inputRef.current.focue()를 실행하라고 조건을 주면 해결되긴 했지만 근본적으로 왜 이런 문제가 발생되는지 알기위해서 먼저 useRef()에 대해 알아보아야 한다 useRef란 useRef는 프로퍼티에 변경 가능한 값을 담고 있는 '상자'..
-
프로그래머스 - 체육복Algorithm 2023. 5. 6. 23:01
문제 풀이 이번 문제는 제법 시간이 꽤 걸렸었다. 풀고자 했던 의도는 먼저 reserve 배열의 요소중 lost 배열의 요소와 같은것이 있는지 즉, 여분의 체육복이 있으면서 도난당한 학생이 있는지 확인하고 해당 학생번호를 lost 배열과 reserve 배열 각각 제거시켜 주었다. 그 후 reserve 배열의 학생번호 앞 뒤 번호가 lost 배열에 있는지 체크하고 있다면 체육복을 가진 학생 수를 증가시켜준다. 하지만, 이론으로는 그럴듯 해 보였으나 계속 한 두 케이스에서 실패가 나서 왜 그런지 이유를 도저히 모르던 와중 lost 배열이나 reserve 배열의 학생 번호가 정렬되어 있지 않아서 발생하는 문제가 있었다. 5명의 학생이 있고, lost 배열에는 [2, 4], reserve 배열에는 [3, 1]이..
-
프로그래머스 - [카카오 인턴] 키패드 누르기Algorithm 2023. 5. 6. 00:21
문제 풀이 카카오 인턴쉽 문제답게 상당히 어려운 문제였다. 풀고자 했던 의도는 키패드를 먼저 2차원 평면 좌표로 구현하기 위해 2차원 배열로 좌표들을 지정해 주었고, 현재 왼손 시작 좌표, 오른손 시작 좌표를 지정해 준 다음 numbers 배열들을 좌표에 매칭시켰다. 먼저 1번, 4번, 7번에 해당되는 (1, 0), (2, 0), (3, 0)일 때 "L"을 추가하고 3번, 6번, 9번에 해당되는 (1, 2), (2, 2), (3, 2)일 때 "R"을 추가시킨다. 남은 0번, 2번, 5번, 8번을 처리할 때가 관건인데 이는 예시를 통해 이해해보자 왼손 위치가 키패드4번, 오른손 위치가 키패드9번에 있을 때 다음으로 5번 키를 눌러야 하는 상황 9번에서 5번을 가기위해 걸리는 거리는 (1,2)와 (2,1)사..
-
프로그래머스 - 두 개 뽑아서 더하기Algorithm 2023. 5. 4. 20:10
문제 풀이 풀고자 했던 의도는 단순하게 2중으로 배열을 순회하면 된다고 생각했다. 단 첫 번째 numbers 배열 안에서 순회되는 두번째 배열은 첫 번째 배열의 인덱스와 다른 인덱스 값과 더해야 하고, 이를 반복하면 2차원 배열이 결과값으로 나오기 때문에 flat 메서드로 1차원 배열로 변환해준 다음 중복값 제거와 정렬을 진행해준다 입출력 예제 2번을 예를 들어 보면 [5, 0, 2, 7] 배열을 순회하는데 5를 순회할 때는 [0, 2, 7], 0은 [5, 2, 7], 2는 [5, 0, 7], 7은 [5, 0, 2] 이런식으로 순회하는 인덱스를 제거한 배열들과 합을 구하는 것이다