오늘 배운 것

오늘은 프로그래머스 사이트에 있는 레벨1 문제중 5문제를 직접 풀어보고 강사님이 리뷰 해주시는 시간을 가졌고 코딩 테스트 문제 유형에 따라 사용될 자바스크립트의 기능들 문자관련 기능, 숫자 및 수학 관련 기능, 배열 함수들을 배웠고 객체에 관련된 문법인 구조분해할당, 전개 연산자, JSON에 대해서도 배웠다.

 

배운 내용 정리

1. 문자 관련 기능

1) 문자열 길이

문자열.length();

const str = "Hello, world";
console.log(str.length);

2) 문자열 위치 찾기, indexOf

문자를 찾을 문자열.indexOf('찾을 문자열');

const str = "Hello, world";
console.log(str.indexOf("world"));

3) 문자열 자르기, slice

문자열.slice(시작 위치, 종료 위치);

const str = "Hello, world";
console.log(str.slice(0, 5));

4) 문자열 바꾸기, replace

문자열.replace('찾을 문자', '바꿀 문자');

const str = "Hello, world";
console.log(str.replace("world", "뽀로로"));

5) 문자열 반복하기, repeat

문자열.repeat(반복 횟수);

let str = "우영";
let longStr = str.repeat(10) + "우";
console.log(longStr);

6) 앞 뒤 공백 문자 제거하기, trim

문자열.trim();

const str = " Hello, world ";
console.log(str.trim());

 

2. 숫자 및 수학 관련 기능

1) 숫자들의 종류, int / float

const pi = 3.14159265358979;
console.log(pi);
let integer = parseInt(pi);
let float = parseFloat(pi);
console.log(integer);
console.log(float);
console.log(typeof integer, typeof float);

정수형 변환: parseInt()

부동소수 변환: parseFloat()

2) 수학 함수들

console.log("abs: ", Math.abs(-999)); // 절대값
console.log("min: ", Math.min(10, 33)); // 작은 값
console.log("min: ", Math.min(10, 33, 6, 2, -1)); // 작은 값
console.log("max: ", Math.max(10, 33)); // 큰 값
console.log("max: ", Math.max(10, 33, 2, 1, 77)); // 큰 값
console.log("ceil: ", Math.ceil(3.14)); // 올림
console.log("floor: ", Math.floor(3.14)); // 버림
console.log("round: ", Math.round(3.6)); // 반올림
console.log("round: ", Math.round(3.4)); // 반올림
console.log("random: ", Math.random()); // 랜덤

 

3. 배열 관련 기능

1) 배열 추가, 삭제

let numbers = [1, 2, 3, 4, 5, 6];
// 원본 배열이 변함
// 마지막에 추가 push
numbers.push(7);
console.log(numbers);
// 처음에 추가 push
numbers.unshift(0);
console.log(numbers);
// 마지막 요소 삭제 & 반환 pop
console.log(numbers.pop());
// 첫 요소 삭제 & 반환 shift
console.log(numbers.shift());

2) 배열 합치기

배열.concat()

let numbers = [1, 2, 3, 4];
let fruits = ["사과", "딸기", "수박"];
console.log(numbers.concat(fruits));
console.log(numbers);
console.log(fruits);

3) 배열 반복

배열명 또는 [].forEach

let numbers = [1, 2, 3, 4, 5, 6];
let fruits = ["사과", "바나나", "수박", "포도", "파인애플"];
numbers.forEach(function (number, index, array) {
console.log(number, index, array);
});

numbers.forEach((number, index, array) => {
console.log(number, index, array);
});

4) 배열의 데이터를 반환 + 배열로 만들어줌

배열명 또는 [].map

let numbers = [1, 2, 3, 4, 5, 6];

let map = numbers.map(function (number) {
return number;
});

let map2 = numbers.map(number => {
return number;
});

console.log("map :", map);
console.log("map2 :", map2);

5) 배열 값 누적

배열명 또는 [].reduce

let numbers = [1, 2, 3, 4, 5, 6];
let reduce = numbers.reduce(function (sum, item, index, arr) {
console.log(sum, item, index, arr);
return sum + item;
})

6) 조건에 부합하는 배열 요소를 반환 filter

배열명 또는 [].filter((매개변수) => 조건)

let numbers = [1, 2, 3, 4, 5, 6];
let arr;
arr = numbers.filter((num) => num > 3);
console.log(arr);

const words = ['spray', 'limit', 'elite', 'exuberant',
'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);

 

'코딩온(부트캠프 학원) - TIL' 카테고리의 다른 글

22.07.29- TIL  (0) 2022.07.31
22.07.25 - TIL  (0) 2022.07.26
22.07.20- TIL  (0) 2022.07.21
22.07.18- TIL  (0) 2022.07.18
22.07.15- TIL  (0) 2022.07.15

+ Recent posts