Javascript Math를 알아보자

JD·2021년 9월 28일
0

JavaScript

목록 보기
11/13

📢 하기전에

Math는 수학적인 상수,함수를 위한 속성과 메서드를 가진 내장객체이다
floor,random을 쓰는 편인데 Math를 배우고 알고 쓰자!

📢 주요 사용 속성,메서드

let s;

s = Math.PI;//원주율
console.log(s);//3.14...

s = Math.round(10.2); //숫자에 가장 가까운 정수 
console.log(s);//10

s = Math.ceil(10.2); //인수보다 크거나 같은 수중 가장 작은 정수 (올림)
console.log(s);//11

s = Math.floor(10.2); //인수보다 적거나 같은 수중 가장 큰 정수 (절삭)
console.log(s);//11

s = Math.random(); //0<= 난수 <1 
console.log(s);

s = Math.floor( Math.random()*10 ); 
console.log(s);// 0~9사이 난수 출력

s = Math.pow(2,10); //2의 10승 **로 사용가능하기 때문에 사용빈도가 적다  
console.log(s);

📢 난수를 이용한 배경색바꾸기

function bg() {
	var r = Math.floor(Math.random()*256);
	var g = Math.floor(Math.random()*256);
	var b = Math.floor(Math.random()*256);
	
	var layout = document.getElementById("box");
	layout.style.backgroundColor = 'rgb('+r+','+g+','+b+')';
	setTimeout("bg()",500);
}

📢Reference

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math

📢 쓰면서

Math의 메소드는 정적메소드라서 Math.floor()를 사용해야한다

📢 마치며

알고 쓰는거랑 모르고 쓰는거는 차이가 심한거같아 처음부터 시작한건데 내가 너무 모르는게 많다 더 알고싶다

0개의 댓글