본문 바로가기

Bootcamp/JavaScript4

[algorithm] 배열 회전 Rotation of a Matrix 문제 2차원 N x N 배열을 시계 방향으로 90도 회전시킨 배열을 구한다. 입출력 예시 const matrix = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], ]; console.log(matrix[0][0]); // --> 1 console.log(matrix[3][2]); // --> 15 const rotatedMatrix = rotateMatrix(matrix); console.log(rotatedMatrix[0][0]); // --> 13 console.log(rotatedMatrix[3][2]); // --> 8 코드(JavaScript) const rotateMatrix = function (matrix, rotateN.. 2022. 1. 12.
JavaScript, 여러 개의 함수를 합성하기 [문제] 여러 개의 함수를 순차적으로 연속적으로 적용할 경우, 여러 함수의 기능을 합친 기능을 수행하는 단 하나의 함수를 만들고 싶다. function square(num) { return num * num; } function add5(num) { return num + 5; } function mul3(num) { return num * 3; } function isOdd(num) { return num % 2 !== 0; } // 중첩된 함수들을 합성하는 pipe 라는 이름의 함수를 만들어보자 function pipe() { return init => { let result = init; for(let func of arguments) { result = func(result) } return re.. 2021. 10. 28.
JavaScript, 두 함수의 합성함수 만들기 [문제] function1, function2 두 개의 함수가 있을 때, 두 함수를 차례대로 적용한 효과를 갖는 하나의 함수를 만들고 싶다 예를 들어 3을 더해주는 함수와 제곱을 해주는 함수가 있을 때, 3을 더해 제곱해주는 함수를 만들고 싶다. const add3 = x => x + 3; const square = x => x ** 2; const composedFunction = (fn1, fn2) => { return num => fn2(fn1(num)); } const func1 = composedFunction(add3, square); const func2 = composedFunction(square, add3); console.log('3을 더하고 제곱한 결과는 ', func1(4)); c.. 2021. 10. 28.
String.prototype.replace()와 정규표현식 [문제 1] 공백을 포함하는 문자열이 주어졌을 때, 공백 다음의 문자를 모두 대문자로 바꾸려고 한다. 예시: 'hello world' --> 'Hello World' 여러가지 방법이 있겠지만, 정규표현식을 이용해 해결하려고 한다. 대문자로 바꾸는 함수 String.prototype.toUpperCase()를 사용한다. toUpperCase()를 적용할 대상을 찾을 때 정규표현식을 사용한다. /^\w|(? 2021. 10. 28.