코드 카타를 부셔보자!💥

김인태·2022년 6월 8일
0

전에도 이러한 게시물이 있지만 더욱 필요한 것 같아서
코딩테스트에 필요한 메소드를 정리해보았다!!!
승리를위해서.. 몇번씩 머리로 정리해서 필요할 때 적재적소에 이용해보도록하자!

Number.MIN_SAFE_INTEGER / Number.MAX_SAFE_INTEGER

const x = Number.MIN_SAFE_INTEGER - 1;
const y = Number.MIN_SAFE_INTEGER - 2;

console.log(Number.MIN_SAFE_INTEGER);
// expected output: -9007199254740991

console.log(x);
// expected output: -9007199254740992

console.log(x === y);
// expected output: true
const x = Number.MAX_SAFE_INTEGER + 1;
const y = Number.MAX_SAFE_INTEGER + 2;

console.log(Number.MAX_SAFE_INTEGER);
// expected output: 9007199254740991

console.log(x);
// expected output: 9007199254740992

console.log(x === y);
// expected output: true

자바스크립트에서 안전한 최대, 최소 정수값을 나타냅니다.

문자열 변환에 관한 메소드

str.toString() / str.toUpperCase() / str.toLowerCase()

toString()

var arr = [1, 2, 3];
arr.toString(); // 1,2,3 반환

배열에다가 toString 메소드를 사용하면 1,2,3이 반환된다!

toUpperCase , toLowerCase

const sentence = 'The quick brown fox jumps over the lazy dog.';

console.log(sentence.toUpperCase());
// expected output: "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."

보시다시피 소문자를 대문자로 바꿔주는 함수이고, toLowerCase는 당연히 소문자로 바꿔주는 함수입니다.

str.substring(index1, index2)

substring()

const str = 'Mozilla';

console.log(str.substring(1, 3));
// expected output: "oz"

console.log(str.substring(2));
// expected output: "zilla"

string 객체의 시작 인덱스로 부터 종료 인덱스 까지 문자열의 부분 문자열을 반환합니다.

str.indexOf('문자') / array.indexOf('배열')

 !String인 경우!
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf(searchTerm);

console.log(`The index of the first "${searchTerm}" from the beginning is ${indexOfFirst}`);
// expected output: "The index of the first "dog" from the beginning is 40"

console.log(`The index of the 2nd "${searchTerm}" is ${paragraph.indexOf(searchTerm, (indexOfFirst + 1))}`);
// expected output: "The index of the 2nd "dog" is 52"

////////////////////
 ! 배열인 경우 
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];

console.log(beasts.indexOf('bison'));
// expected output: 1

// start from index 2
console.log(beasts.indexOf('bison', 2));
// expected output: 4

console.log(beasts.indexOf('giraffe'));
// expected output: -1

변수로 주어진 문자열 'dog' 가 있는 첫번 째 인덱스를 반환합니다. 일치하는 값이 없으면 -1을 반환합니다.
배열인 경우 첫번 째 것을 예시로 들자면 배열 요소인 bison이 있는 index를 반환해서 1이 되는겁니다 !

filter()

array.filter((요소, index) => { })

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

주어진 함수의 테스트를 통과하는 모든 요소를 배열로 반환합니다!
예제와 같은 경우는 word의 length가 6보다 큰 요소들을 배열에 모아서 배열로 반환했습니다!

sort()

const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// expected output: Array ["Dec", "Feb", "Jan", "March"]

const array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);
// expected output: Array [1, 100000, 21, 30, 4]

배열의 요소를 적절한 위치에 정렬한 후 그 배열을 반환합니다!
구현 방식에 따라 다른 위치로 정렬하는데
오름차순(작은순서)정렬 -> Array.sort((a,b) => a - b));
내림차순(큰순서)정렬 -> Array.sort((a,b) => b - a));

join()

const elements = ['Fire', 'Air', 'Water'];

console.log(elements.join());
// expected output: "Fire,Air,Water"

console.log(elements.join(''));
// expected output: "FireAirWater"

console.log(elements.join('-'));
// expected output: "Fire-Air-Water"

join은 배열의 모든 요소를 연결해 하나의 문자열로 만듭니다.(배열을 입력받아 문자열로 리턴)

replace()

const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';

console.log(p.replace('dog', 'monkey'));
// expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"


const regex = /Dog/i;
console.log(p.replace(regex, 'ferret'));
// expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"

어떤 패턴에 일치하는 일부나 모든 부분이 교체된 문자열을 반환합니다.
예제와 같은 경우 변수 p에 있는 dog가 monkey로 교체된 결과를 보여줍니다.

isNaN()

function milliseconds(x) {
  if (isNaN(x)) {
    return 'Not a Number!';
  }
  return x * 1000;
}

console.log(milliseconds('100F'));
// expected output: "Not a Number!"

console.log(milliseconds('0.0314E+2'));
// expected output: 3140

어떤 값이 NaN인지 판별합니다.
Number이면 true를 반환하고, Number가 아니면 false를 반환합니다

이 블로그 게시물을 지하철 탈 때마다 보고 자동으로 나올 수 있게 하겠습니다.........

profile
새로운 걸 배우는 것을 좋아하는 프론트엔드 개발자입니다!

0개의 댓글