Algorithm 뽀개기/알고리즘을 위한 문법

[JavaScript 알고리즘을 위한 문법] 01. Math.max() Math.min()

딸기케잌🍓 2023. 5. 21. 19:15

Math.max(), Math.min()

입력값으로 받은 0개 이상의 숫자 중 가장 큰(작은) 숫자를 반환합니다.

console.log(Math.max(-1, -3, -2)); // -1

const array1 = [1, 3, 2];
console.log(Math.max(...array1)); //3

문자열이 들어가도 됨

리턴하는 값의 타입은 Number

  const str = "-1 -2 -3 -4";
  const arr = str.split(" ");
  console.log(Math.max(...arr)); //-1
  console.log(typeof Math.max(...arr));number

 

 

 

Math.ceil()

- 올림

- always rounds up and returns the smallest integer greater than or equal to a given number.

 

console.log(Math.ceil(0.95));
// Expected output: 1

console.log(Math.ceil(4));
// Expected output: 4

console.log(Math.ceil(7.004));
// Expected output: 8

console.log(Math.ceil(-7.004));
// Expected output: -7

Math.ceil(-7.004); // -7
Math.ceil(-4); // -4
Math.ceil(-0.95); // -0
Math.ceil(-0); // -0

 

 

 

 

Math.round()

- 반올림

- returns the value of a number rounded to the nearest integer.

console.log(Math.round(0.9));
// Expected output: 1

console.log(Math.round(5.95), Math.round(5.5), Math.round(5.05));
// Expected output: 6 6 5

console.log(Math.round(-5.05), Math.round(-5.5), Math.round(-5.95));
// Expected output: -5 -5 -6

 

 

Math.floor()

-내림

- always rounds down and returns the largest integer less than or equal to a given number.

Math.floor(-Infinity); // -Infinity
Math.floor(-45.95); // -46
Math.floor(-45.05); // -46
Math.floor(-0); // -0
Math.floor(0); // 0
Math.floor(4); // 4
Math.floor(45.05); // 45
Math.floor(45.95); // 45
Math.floor(Infinity); // Infinity