반응형
난수
Math.random(): 0과 1사이의 난수 발생
Math.floor(Math.random() * 100) + 1;
- 1부터 100사이의 난수가 리턴됨
- 100대신에 원하는 범위의 최대값을 넣으면 난수를 구할 수 있음
Math 매서드 종류 및 설명
- Math.abs(x): returns the absolute value of x
console.log(Math.abs(-5)); // Output: 5
- Math.sqrt(x): returns the square root of x
console.log(Math.sqrt(16)); // Output: 4
- Math.pow(x, y): returns x raised to the power of y
console.log(Math.pow(2, 3)); // Output: 8
- Math.random(): returns a random number between 0 and 1
console.log(Math.random()); // Output: a random number between 0 and 1
- Math.floor(x): returns the largest integer less than or equal to x
console.log(Math.floor(3.14)); // Output: 3
- Math.ceil(x): returns the smallest integer greater than or equal to x
console.log(Math.ceil(3.14)); // Output: 4
- Math.round(x): rounds x to the nearest whole number
console.log(Math.round(3.14)); // Output: 3
- Math.min(x1, x2, ...): returns the smallest number from a set of numbers
console.log(Math.min(5, 2, 7, 1, 8)); // Output: 1
- Math.max(x1, x2, ...): returns the largest number from a set of numbers
console.log(Math.max(5, 2, 7, 1, 8)); // Output: 8
반응형
'Javascript' 카테고리의 다른 글
| Number Methods (0) | 2023.01.24 |
|---|---|
| 배열 (0) | 2023.01.24 |
| if else 조건문 (0) | 2023.01.24 |
| switch 조건문 (0) | 2023.01.24 |
| for do while 반복문 (0) | 2023.01.24 |
댓글