숫자 관련 함수 math.***
- Math is a built-in object that has properties and methods for mathematical constants and functions. It's not a function object.
- Math works with the Number type. It doesn't work with BigInt.
- Unlike many other global objects, Math is not a constructor. All properties and methods of Math are static
Static Methods
✔ math.ceil
always rounds a number up to the next largest integer. Returns the smallest integer greater than or equal to x
console.log(Math.ceil(.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
*note: Math.ceil( null ) returns integer 0 and does not give a NaN error
✔ math.floor
This function returns the largest integer less than or equal to a given number.
console.log(Math.floor(5.95)); // expected output: 5
console.log(Math.floor(5.05)); // expected output: 5
console.log(Math.floor(5)); // expected output: 5
console.log(Math.ceil(-5.05)); // expected output: -6
✔ math.random
returns a floating-point, pseudo-random number in the range 0 to less than 1 ( inclusive of 0, but not 1 ) with approximately uniform distribution over that range - which you can then scale to your desired range.
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
console.log(getRandomInt(3)); // expected output: 0, 1 or 2
console.log(getRandomInt(1)); // expected output: 0
console.log(Math.random()); // expected output: a number from 0 to <1
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
- 회원 데이터 랜덤하게 넣기 위해 사용하였다. Math.floor + Math.random 조합을 많이 쓴다
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
// 랜덤 ID, PW 만들기
let id = makeId(5);
let pw = makeId(6);
const result = await signUp(id, pw, name, age, level, cardNum, address);
};
const makeId = (length: number) => {
let result = '';
let characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
};
const age = random(2);
const cardNum = random(16);
const random = (length: number) => {
let result = '';
let characters = '0123456789';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return Number(result);
};
const name = makeName();
const makeName = () => {
let result = '';
let characters = '가나다라마바사아자카타파하리안류진정엠유예지채령연송한별은원';
for (let i = 0; i < 2; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
};
const levelArray = ['새싹', '잎새', '나무'];
const level = levelArray[Math.floor(Math.random() * 3)];
const addressArray = [
'서울 노원구',
'서울 영등포구',
'서울 송파구',
'서울 양천구',
'서울 마포구',
'대전 대덕구',
'부산 해운대구',
'대구 달서구',
'서울 강북구',
'서울 성북구',
];
const address = addressArray[Math.floor(Math.random() * 10)];
- 두더지 잡기 게임에서 randomPosition 세팅할 때 사용하였다
function randomSquare() {
square.forEach((className) => {
className.classList.remove("mole");
});
let randomPosition = square[Math.floor(Math.random() * 9)];
randomPosition.classList.add("mole");
// assign the id of the randomPosition to hitPosition for us to use later
hitPosition = randomPosition.id;
}
🔗 https://github.com/salybu/games.js/blob/master/whack-a-mole/app.js
✔ math.round
This function 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
참고
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
.