분류 전체보기

https://programmers.co.kr/learn/courses/30/lessons/70128 코딩테스트 연습 - 내적 길이가 같은 두 1차원 정수 배열 a, b가 매개변수로 주어집니다. a와 b의 내적을 return 하도록 solution 함수를 완성해주세요. 이때, a와 b의 내적은 a[0]*b[0] + a[1]*b[1] + ... + a[n-1]*b[n-1] 입니다. (n은 a, b의 programmers.co.kr 왜 reduce로 바로하는 방법을 생각하지 못했을까 ? 둘 다 배열로 주어지니 reduce사용해야겠다 바로 떠올려보자 나의 코드 function solution(a, b) { let box = []; for(i=0, k=0; i
https://programmers.co.kr/learn/courses/30/lessons/76501 코딩테스트 연습 - 음양 더하기 어떤 정수들이 있습니다. 이 정수들의 절댓값을 차례대로 담은 정수 배열 absolutes와 이 정수들의 부호를 차례대로 담은 불리언 배열 signs가 매개변수로 주어집니다. 실제 정수들의 합을 구하여 re programmers.co.kr 한 2분이면 풀겠다 했는데 20분이 걸렸다. 아직 reduce에 대해서 완변하게 이해 못한 것 같다. 그리고 reduce 에서 index를 적극 활용하자 나의 코드 function solution(absolutes, signs) { const result = absolutes.reduce((pre, cur, i) => { if(signs[..
the "Cannot read property '0' of undefined" error occurs when trying to access the index '0' on a variable that stores an 'undefined' value. 이것을 해결하기 위해서는 array 이나 string 같은 인덱스 엑세스를 지원하는 값에만 엑세스해야한다. 예제 const arr = undefined; // ⛔️ Cannot read properties of undefined (reading '0') console.log(arr[0]); const str = undefined; // ⛔️ Cannot read properties of undefined (reading '0') console.log(str..
 Array.prototype.reduce() 주의 : reduce는 원본배열을 변환시키지 않는다. reduce 메서드는 자신을 호출한 배열을 모든 요소를 순회하며 인수로 전달받은 콜백 함수를 반복 호출한다. 그리고 콜백 함수의 반환값을 다음 순회 시에 콜백 함수의 첫 번째 인수로 전달하면서 콜백 함수를 호출하여 하나의 결과값을 만들어 반환한다. 이때 원본 배열은 변경되지 않는다 reduce 메서드는 자신을 호출한 배열의 모든 요소를 순회하며 하나의 결과값을 구해야 하는 경우에 사용한다. const sum = [1, 2, 3, 4].reduce((accumulator, currentValue, index, array) => accumulator + currentValue, 0) console.log(s..
https://programmers.co.kr/learn/courses/30/lessons/64061 코딩테스트 연습 - 크레인 인형뽑기 게임 [[0,0,0,0,0],[0,0,1,0,3],[0,2,5,0,1],[4,2,4,4,2],[3,5,1,3,1]] [1,5,3,5,1,2,1,4] 4 programmers.co.kr 나의 코드 function solution(board, moves) { var answer = 0; var box = []; let boards = board for( i=0; i < moves.length; i++){ let moving = moves[i] for(k=0; k { return unique[unique.length - 1] === item ? unique.slice(0,-..
https://programmers.co.kr/learn/courses/30/lessons/67256 코딩테스트 연습 - 키패드 누르기 [1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5] "right" "LRLLLRLLRRL" [7, 0, 8, 2, 8, 3, 1, 5, 7, 6, 2] "left" "LRLLRRLLLRR" [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] "right" "LLRLLRLLRL" programmers.co.kr 풀이 처음에는 if문으로 1,4,7 / 3,6,9를 나누고 2,5,8,0은 따로 해줬는데 이렇게 하면 너무 if문이 많아져서 질문하기에서 약간의 힌트를 얻었다. 힌트를 보기전에는 1~12번 캐이스는 전부 성공하는데 13~20번은 전부 실패하였다. 그 이..
배열 배열에서 ...을 사용하면 배열이 개별원소가 된다. const array = [1, 2, 3] console.log(...array) // 1 2 3 console.log([...array]) // [ 1, 2, 3 ] console.log(array) // [1, 2, 3] const array1 = [1, 2] const array2 = [5, 6] const array3 = [...array1, 3, 4, ...array2] console.log(array3) // [ 1, 2, 3, 4, 5, 6] 이런식으로 출력이 된다. 그리고 ...을 사용해도 원본은 그대로 유지가 된다. 객체 객체에서 사용할때는 나머지를 의미한다. const obj = { a : 1, b : 2, c : 3, d : 4..
Argument of type '({ customCategory }: SForm) => void' is not assignable to parameter of type 'SubmitHandler'. Types of parameters '__0' and 'data' are incompatible. Property 'customCategory' is missing in type '{ [x: string]: any; }' but required in type 'SForm'. 이런 오류가 떴다. 즉 타입이 제대로 적용이 되지 않은 것이다. 이런식으로 코드를 만들어줬는데. handleSubmit 을 useForm에서 가져오는 과정에서 타입지정을 안해주고 가져와서 생긴 오류이다. 이렇게 해주면 끝!
https://programmers.co.kr/learn/courses/30/lessons/81301 코딩테스트 연습 - 숫자 문자열과 영단어 네오와 프로도가 숫자놀이를 하고 있습니다. 네오가 프로도에게 숫자를 건넬 때 일부 자릿수를 영단어로 바꾼 카드를 건네주면 프로도는 원래 숫자를 찾는 게임입니다. 다음은 숫자의 일부 자 programmers.co.kr 딱 보고 생각이나지 않았다 이것도 정규식과 replace를 이용해서 바꾸는 건가 ? 그럼 어떤 영단어인지 인식하고 그것을 숫자로 바꿔야하는데 나의코드 function solution(s) { const data = [{zero:"0"}, {one:1}, {two:2}, {three:3}, {four:4}, {five:5}, {six:6}, {seven..
개발자성장기
'분류 전체보기' 카테고리의 글 목록 (29 Page)