Honey-Programming

코플릿 복습3(Math 메서드) 본문

코드스테이츠/ALGORITHM

코플릿 복습3(Math 메서드)

Latte_is_horse 2020. 7. 18. 16:02
Math 메서드 : 수학적인 상수와 함수를 위한 속성과 메서드를 가진 내장 객체 

1) 속성(Property)
 1. Math.PI : 원의 둘레와 지름의 비율. 즉, π :3.141592
 2. Math.SQRT1_2 : ½의 제곱근. 약, 0.707
 3. Math.SQRT2 : 2의 제곱근. 약, 1.414

2) 메서드(Method)
 1. Math.abs(x) : 절대값을 반환
 Ex) Math.abs('-2');    Math.abs(-2);    Math.abs([2]); => 2
     Math.abs(null);    Math.abs('');    Math.abs([]);  => 0
     Math.abs([1, 2]);  Math.abs({});    Math.abs('string');    Math.abs(); => NaN
 
 2. Math.cbrt(x) : 세제곱근을 반환
 Ex) Math.cbrt(8); => 2
 
 3. Math.floor(x) : 가장 큰 정수를 반환
 Ex) Math.floor(45.95);    Math.floor( 45.05); => 45
     Math.floor( 4 );  => 4
     Math.floor(-45.05);    Math.floor(-45.95); => -46
 
 4. Math.max(x, y) : 인수에서 제일 큰 수를 반환
 Ex) Math.max(10, 20);  => 20    Math.max(-10, -20);  => -10    Math.max(-10, 20);  => 20 
 function getMaxOfArray(numArray) {
   return Math.max.apply(null, numArray);
 }
 
 var arr = [1,2,3];
 var max = arr.reduce(function(a, b) {
   return Math.max(a, b);
 });

 var arr = [1, 2, 3];
 var max = Math.max(...arr);
 console.log(getMaxOfArray([12,10,4]));
 
 5. Math.min(x, y) : 인수에서 제일 작은 수를 반환
 Ex) Math.min(10, 20);  => 10    Math.min(-10, -20);  => -20    Math.min(-10, 20);  => -10
 
 6. Math.random() : 0과 1 사이의 난수를 반환
 0이상 1미만의 난수 생성
 function getRandom() {
   return Math.random();
 }
 
 두 값 사이의 난수 생성
 function getRandomArbitrary(min, max) {
   return Math.random() * (max - min) + min;
 }
 
 두 값 사이의 정수 난수 생성
function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min; //최댓값은 제외, 최솟값은 포함
}

 7. Math.pow(x, y) : x의 y 제곱을 반환
 Ex) Math.pow(7, 2)  => 49  Math.pow(7, 3) => 343
     Math.pow(4, 0.5) => 2  Math.pow(8, 1/3) => 2  Math.pow(2, 0.5) => 1.414
     
 8. Math.round(x) : 입력값을 반올림한 값과 가장 가까운 정수를 반환
 Ex) Math.round(20.49) => 20    Math.round(20.5) => 21    Math.round(-20.51) => -21    Math.round(-20.5) => -20
 
 9. Math.sign(x) : 어떤 수의 부호를 반환. 음수인지, 양수인지, 0인지를 나타냄
 Ex) Math.sign(3);  => 1    Math.sign(-3);  => -1    Math.sign('-3');  => -1    Math.sign(0);  => 0
     Math.sign(-0);  => -0  /  Math.sign(NaN, 'foo', ); => NaN

 10. Math.sqrt(x) : 숫자의 제곱근을 반환

 Ex) Math.sqrt(9);  => 3    Math.sqrt(2);  => 1.414    Math.sqrt(1);  => 1    Math.sqrt(-1);  => NaN

 

수학) 5-1_computeAreaOfATriangle (삼각형의 넓이)

삼각형의 밑변과 높이가 주어졌을때, 삼각형의 넓이를 반환

function computeAreaOfATriangle(base, height) {
  return (base * height) / 2
}

// 숫자를 리턴
// 삼각형의 넓이를 리턴

let output = computePower(2, 3);
console.log(output); // --> 8

 

수학) 5-2_computePower (제곱근)

숫자와 지수가 주어졌을때, 숫자의 지수만큼 제곱된 값을 반환

function computePower(num, exponent) {
  return Math.pow(num, exponent);
}

// 숫자를 리턴
// 지수가 0이면 항상 1을 리턴
// 주어진 수가 양수일 때 올바른 값을 리턴
// 주어진 수가 음수일 때 올바른 값을 리턴

let output = computePower(2, 3);
console.log(output); // --> 8

 

수학) 5-3_computePower (원의둘레)

원의 반지름이 주어졌을때, 원의 둘레를 반환

function computePerimeterOfACircle(radius) {
  return 2 * Math.PI * radius;
}

// 숫자를 리턴
// 올바른 원의 둘레를 리턴

let output = computePerimeterOfACircle(4);
console.log(output); // --> 25.132741228718345

 

수학) 5-4_getMaxOfArray (가장 큰 배열)

숫자 배열에서, 최대 요소를 반환

function getMaxOfArray(numArray) {
  return Math.max.apply(null, numArray);
}

 

 

'코드스테이츠 > ALGORITHM' 카테고리의 다른 글

test1 ~ test 7 (보관)  (0) 2020.08.31
코플릿 복습4(형변환, 반복문)  (0) 2020.07.18
코플릿 복습2  (0) 2020.07.12
코플릿 복습1  (0) 2020.07.12
문제풀기  (0) 2020.06.16
Comments