목록공부/JavaScript (33)
저장을 습관화
1. Math.pow() 사용법 Math.pow(a, b) a는 제곱할 수, b는 제곱할 횟수 예시) console.log(Math.pow(4, 1)); // 4 console.log(Math.pow(4, 2)); // 16 console.log(Math.pow(4, 3)); // 64 console.log(Math.pow(4, 4)); // 256 2. ** 사용법 a ** b 동일하게 a는 제곱할 수, b는 제곱할 횟수 예시 console.log(4 ** 1); // 4 console.log(4 ** 2); // 16 console.log(4 ** 3); // 64 console.log(4 ** 4); // 256
자바스크립트의 배열 메소드 중 하나이다. 배열 내 요소들을 하나의 값으로 축소하는데 사용된다. 주로 배열 내 모든 요소들을 더하거나 곱하는 등의 계산에 사용된다. reduce 메소드의 구조 배열.reduce(callback, initialValue) 배열은 말 그대로 메소드를 사용할 배열을 말한다. callback은 각 요소에 대해 호출되는 함수이다. 아래 4개의 인자를 받는다. - accumulator: 누적된 결과 값 - currentValue: 현재 처리 중인 배열 요소 - currentIndex: 현재 처리 중인 배열 요소의 인덱스 - array: reduce가 호출되는 배열 initialValue는 초기 누적값으로 사용할 값이다. 선택사항이며 지정하지 않으면 배열의 첫번째 요소가 초기값으로 사용..
!는 논리 not 연산자이다. true를 false로, false를 true로 바꾼다 +는 더하기 연산자이다. +를 형변환하는데 쓴다면 양 옆에 있는 두 피연산자 중 하나가 문자열일때 덧셈의 결과를 문자열로 바꿀수도 있지만 다른 데이터 유형을 숫자로 형변환하는데도 쓸 수 잇다. 예시) let num = "42"; console.log(+num); // 42 console.log(typeof +num); // number let str = "Hello"; console.log(+str); // NaN console.log(typeof +str); // number 둘을 응용한 +!의 사용 방법 아래와 같은 코드가 있다. function solution(number, n, m) { return +!(numb..
1. slice(start, end) 문자열을 자르는데 사용하는 slice는 배열에도 사용할 수 있다 start와 end의 인덱스를 지정하여 사용하며 배열의 일부를 추출하여 새로운 배열을 반환한다. 원본 배열은 변경되지 않는다. 예시) const fruits = ["apple", "banana", "cherry", "date", "fig"]; const slicedFruits = fruits.slice(1, 3); // 인덱스 1부터 3 앞까지의 요소를 추출 console.log(slicedFruits); // ["banana", "cherry"] console.log(fruits); // [ 'apple', 'banana', 'cherry', 'date', 'fig' ] // 원본 배열은 변경되지 않음..
객체 혹은 배열들을 펼칠 수 있게 해준다. 활용 방법 1. 배열 확장 const arr1 = [1, 2, 3]; const arr2 = [...arr1, 4, 5, 6]; console.log(arr2); // [ 1, 2, 3, 4, 5, 6 ] const arr1 = [1, 2, 3]; const arr2 = [4, 5]; arr1.push(...arr2); console.log(arr1); // [1, 2, 3, 4, 5] 배열 내 최대값을 가진 요소와 최소값을 가진 요소 const arr = [1, 2, 3, 4, 5, 7]; let max = Math.max(...arr); let min = Math.min(...arr); console.log(max, min); // 7 1 2. 객체 확장 c..
사용방법 replace("바꾸고 싶은 문자열", "바꿀 문자열") 기본적으로 가장 먼저 검색되는 1개의 문자열만 변환한다. let str1 = "Hello"; let str2 = "Hello, Hello Hello!"; console.log(str1.replace("Hello", "Hi!")); // Hi! console.log(str2.replace("Hello", "Hi!")); // Hi, Hello1 Hello! 모든 문자열을 변경하고 싶다면 정규식을 사용한다. let str1 = "Hello"; let str2 = "Hello, Hello Hello!"; console.log(str1.replace(/Hello/g, "Hi!")); // Hi! console.log(str2.replace(/He..
1. substr(start, length) start: 추출을 시작할 문자열의 위치 (0부터 시작한다) length: 추출할 문자의 길이 혹은 start 옵션만 입력함으로써 시작지점부터 끝까지 추출할 수 도 있다. 예시) const str = "Hello, World!"; console.log(str.length); // 13 console.log(str.substr(7, 5)); // World console.log(str.substr(7, 6)); // World! console.log(str.substr(7)); // World! 2. substring(start, end) start: 추출을 시작할 문자열의 위치 end: 추출을 종료할 문자열의 위치 (지정된 인덱스 바로 전까지 추출한다.) 추출..
정규 표현식을 사용하여 문자열을 검사한다. 주어진 정규 표현식에 문자열이 적합하다면 참을 반환하고, 아니라면 거짓을 반환한다. 주로 문자열이 특정 패턴을 만족하는지 확인할 때 사용한다. 예시 1) const pattern = /^[0-9]+$/; // 숫자 패턴 const str = "12345"; console.log(pattern.test(str)); // true 예시 2) const pattern = /[a-z]/; // 소문자 검사 패턴 const str1 = "abcdef"; const str2 = "ABCDEF"; console.log(pattern.test(str1)); // true console.log(pattern.test(str2)); // false 응용 1) 핸드폰 번호 패턴 검..
배열의 모든 요소를 하나의 문자열로 결합한다. 예시 1) const fruits = ["apple", "banana", "cherry"]; const result = fruits.join(); // 배열 요소를 쉼표와 공백으로 구분하여 결합 console.log(result); // "apple,banana,cherry" 이때 .join()의 옵션에 구분 방법을 입력하여 요소 사이사이에 삽입할 수 있다. 디폴트는 ,이며 띄어쓰기는 지원하지 않는다. 예시 2) const fruits = ["apple", "banana", "cherry"]; const result = fruits.join(""); // 배열 요소를 쉼표와 공백으로 구분하여 결합 console.log(result); // "applebana..
문자열 변수 str이 있을때 let str = "abcdefg"; console.log(str[0]); // a console.log(str.charAt(2)); // c 배열처럼 str[]로 찾고 싶은 자리의 위치를 정하던가 .charAt() 메소드를 사용하던가 참조 https://ctrs.tistory.com/191 프로그래머스 LV.0 대소문자 바꿔서 출력하기 프로그래머스 LV.0 대소문자 바꿔서 출력하기 https://school.programmers.co.kr/learn/courses/30/lessons/181949 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 ctrs.tistory.com