[코테] 6.8일 복습용 기록

변진상·2023년 6월 9일
0

코딩테스트 준비

목록 보기
3/8

복습용 기록

Math

Math.ceil()
Math.round()
Math.floor()
Math.sqrt() // 제곱근
Math.min() // 배열을 넘겨줄 때는 전개연산자 Math.max(..arr)
 

sort

sort((a,b) => a-b)

a가 b보다 크면 위치 변경.
-> 오름차순

큰 수로 변수 초기화하기

Number.MAX_SAFE_INTEGER

배열 내에 원하는 값의 개수

  1. for
  2. filter
count = arr.filter(ele => ele === '원하는 값').length
  1. reduce
count = arr.reduce((cnt, ele) => cnt + ('a'===ele), 0)


true의 경우 숫자형으로 캐스팅시 1이 되는 원리를 이용한 방식

배열의 메서드 forEach, map

forEach

arr.forEach((value,index)=> {
  console.log(value,index) // => arr[0] 0 ...
  console.log(this) // => [value1, value2, ...]
}, [value1, value2, ...])

두 번째 인자인 thisArg는 생략 가능하다.

map

이해를 돕기 위한 map의 pseudo 코드


const arr = [1, 2, 3, 4]

const res = arr.map((value,index) => {
	return v*v;
})
console.log(res) // => [1, 4, 9, 16]

문자열 내 문자열 글자 찾아내 바꾸기

// BANANA NA를 #으로 바꾸는 예제
str = str.replace(/NA/g, '#'); // => B##

x.charCodeAt(): 문자열 아스키 코드 알아내는 메소드

substring(idx, idx+n): idx부터 idx+n 전 까지의 문제열 리턴

profile
자신을 개발하는 개발자!

0개의 댓글