숫자의 소수점 처리 method
let num = 99.11;
Math.ceil(num) // 100 출력, 올림 적용
Math.floor(num) // 99 출력, 버림 적용
let num = 99.5;
Math.round(num) // 100출력, 반올림 적용
let num = 99.9876543;
num.toFixed(0); // 100 출력
num.toFixed(5); // 99.98765 출력
문자열 안에 숫자가 있는지 판별해서, 그 숫자들의 합을 구하는 코드
function numberSearch(str) {
const digits = '0123456789'; // 숫자로 구성된 문자열을 만들어 줌
let sum = 0;
let pureStr = '';
for (let i = 0; i < str.length; i += 1) {
if (digits.includes(str[i])) { // 숫자인 경우
sum = sum + Number(str[i]);
String.replace(바꾸고 싶은 스트링, 대신 넣으려고 하는 스트링)
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
const regex = /dog/gi;// -> /g 는 global 이란 의미로, 해당 단어가 들어가는 모든 부분을 바꿈. i 는 ignore 인데, 이건 왜 쓰는지 모르겠음.
console.log(p.replace(regex, 'ferret')); //위에 dog 다음에 /g가 들어갔기 때문에 dog 가 모두 바뀜
// expected output: "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"
console.log(p.replace('dog', 'monkey')); // 이건 /g 가 없기 때문에, 제일 처음 나온 dog 만 한번 바뀜
// expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
함수 실행 시
consol.log : 일종의 일시적 결과 확인
return : 함수의 결과값을 내보내면서 해당 함수가 계산이 끝났음을 의미
String.substring(start point, end point 인데 이거 바로 앞까지 짜름)
const str = 'Mozilla';
console.log(str.substring(1, 3)); //1번 자리부터 3번자리 바로 앞인 2번까지 짜름
// expected output: "oz"
console.log(str.substring(2)); // start point 만 넣어주면 시작점부터 끝까지 쭉 보여줌
// expected output: "zilla"
반복문 중 while 반복문
for 와 달리 while 은 끝이 정해져 있지 않은 상황에 쓰면 좋다.
let sum = 1;
let n = 2: // 초기화
while (n <= 4) { // 조건문
sum = sum + n;
n = n + 1; // 증감문
}
console.log(sum); // 10
Array.reverse()
Ex.1)
const a = [1, 2, 3];
console.log(a); // [1, 2, 3]
a.reverse();
console.log(a); // [3, 2, 1]
Ex.2)
const a = {0: 1, 1: 2, 2: 3, length: 3};
console.log(a); // {0: 1, 1: 2, 2: 3, length: 3}
Array.prototype.reverse.call(a); //same syntax for using apply()
console.log(a); // {0: 3, 1: 2, 2: 1, length: 3} //key 값은 그대로이고, value 만 바뀜, 그러나 'length' 는 다른 종류의 key 값이라 적용되지 않음.
2 중 for 문은 2차원 배열에 사용하면 좋다
debugger 는
1/ 함수를 먼저 선언
2/ debugger;
함수 실행
Array.slice(start point, end point (end not included. 이거 바로 앞자리 까지 임))
-> Substring 과 유사한데, 말그대로 Substring 은 스트링에 사용. 이건 배열에 사용
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]