π¨ Frontend/JavaScript
π¦Vanilla JS β€ κ°μ²΄ - (2).4 νμ€ λ΄μ₯ κ°μ²΄ : Map, Math, λλ€ μμ
kongmi
2023. 3. 29. 17:55
Map
Mapμ
μλ°μ€ν¬λ¦½νΈμμ μ 곡νλ λ°μ΄ν° ꡬ쑰 μ€ νλλ‘, key - value μμΌλ‘ λ°μ΄ν°λ₯Ό μ μ₯νκ³ κ΄λ¦¬ν©λλ€.Mapμ
μ κ°μ²΄μ μ μ¬νμ§λ§, κ°μ²΄μλ λ€λ₯΄κ²key
μ λ€μν μλ£νμ μ¬μ©ν μ μμΌλ©°, μμ μλ£νλkey
μ μ¬μ©ν μ μμ΅λλ€.
set(key, value)
:Map
μ κ°μ²΄μ key - value μμ μΆκ°ν©λλ€.get(key)
:Map
μ κ°μ²΄μμ μ§μ ν keyμ λν κ°μ λ°νν©λλ€.has(key)
:Map
μ κ°μ²΄μμ μ§μ ν keyκ° μ‘΄μ¬νλμ§ μ¬λΆλ₯Ό λ°νν©λλ€.delete(key)
:Map
μ κ°μ²΄μμ μ§μ ν keyμ λν key - value μμ μ κ±°ν©λλ€.clear()
:Map
μ κ°μ²΄μμ λͺ¨λ key- value μμ μ κ±°ν©λλ€.size
:Map
μ κ°μ²΄μ μ μ₯λ key - value μμ κ°μλ₯Ό λ°νν©λλ€.
let map = new Map();
map.set("name", "μ λ");
map.set("email", "yuna@naver.com");
map.set("addr", "μμΈμ κ°λ¨κ΅¬");
π μλλ μ 보λ₯Ό λ°±μλμμ λ°μμμ Map
μΌλ‘ λΏλ¦¬μ§λ§, μ§κΈμ λ°μμ¬ μ λ³΄κ° μκΈ° λλ¬Έμ set
μ μ¬μ©νμ¬ κ°μ Έμ€κ² μ΅λλ€.
console.log(map.size); // 3
console.log(map.get("name")); // μ λ
console.log(map.get("email")); // yuna@naver.com
map.forEach((item) => {
console.log(item);
});
/*
μ λ
yuna@naver.com
μμΈμ κ°λ¨κ΅¬ */
Math
- μν μ°μ°μ λ€λ£¨λ
Math
κ°μ²΄ ( μλ°λ λκ°μ )
Math.abs(x)
: xμ μ λκ° λ°νMath.ceil(x)
: x μ΄μμ κ°μ₯ μμ μ μ λ°νMath.floor(x)
: x μ΄νμ κ°μ₯ ν° μ μ λ°νMath.round(x)
: xλ₯Ό λ°μ¬λ¦Όν μ μ λ°νMath.max(x1, x2, ... , xn)
: μ£Όμ΄μ§ κ°λ€ μ€ κ°μ₯ ν° κ° λ°νMath.min(x1, x2, ... , xn)
: μ£Όμ΄μ§ κ°λ€ μ€ κ°μ₯ μμ κ° λ°νMath.pow(x, y)
: xμ y μ κ³± λ°νMath.sqrt(x)
: xμ μ κ³±κ·Ό λ°νMath.random()
: 0 μ΄μ 1 λ―Έλ§ λμ λ°ν
console.log(Math.abs(-5)); // 5
console.log(Math.ceil(1.1)); // 2
console.log(Math.floor(1.9)); // 1
console.log(Math.round(1.4)); // 1
console.log(Math.max(1, 2, 3)); // 3
console.log(Math.min(1, 2, 3)); // 1
console.log(Math.pow(2, 3)); // 8
console.log(Math.sqrt(9)); // 3
console.log(Math.random()); // 0 μ΄μ 1 λ―Έλ§μ μμμ μ
Math.random() : 0λ³΄λ€ ν¬κ³ 1λ³΄λ€ μμ μ«μν λ°ν
let a = Math.floor(Math.random() * 10); // 0 ~ 9 μ μκ° λ°ν
console.log(a);
1 ~ 10 μ¬μ΄μ μ μκ° λ°ν
let b = parseInt((Math.random() * 10) + 1); // 1 ~ 10 μ μκ° λ°ν
console.log(b);
κ°λ¨ν μμ ) λΉμ²¨μ μΆμ²¨!
- μλͺ¨μ μλ₯Ό μ λ ₯ λ°μ λΉμ²¨μλ₯Ό λ°ν νλ νλ‘κ·Έλ¨ μμ±
- μλͺ¨μ μ :
input
μ λ ₯λ°κΈ° - λΉμ²¨μ λ°ν : μλͺ¨μ μ μ¬μ΄μμ λΉμ²¨μλ₯Ό μ ν(λλ€)ν΄ λ°ν
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
box-sizing: border-box;
margin: 0;
}
h1 {
margin-top: 30px;
text-align: center;
}
.box {
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.item {
margin: 20px 0;
}
#rst {
border-radius: 10px;
width: 300px;
height: 80px;
background-color: navy;
line-height: 80px;
color: #fff;
font-size: 2em;
font-weight: bold;
}
button:hover {
background-color: yellow;
}
</style>
</head>
<body>
<h1>μκΈ μ΄λ²€νΈ μΆμ²¨</h1>
<div class="box">
<div class="item">
<label for="num">μ΄ μλͺ¨μ μ</label>
<input type="text" id="num">
<button onclick="random()">ν΄λ¦!!</button>
</div>
<p id="rst"></p>
</div>
<script src="./230329_1_5_μλͺ¨μλλ€.js"></script>
</body>
</html>
JavaScript
function random() {
const num = Number(document.getElementById("num").value);
let ran = parseInt((Math.random() * num) + 1);
document.getElementById("rst").innerHTML = `${ran}λ² λΉμ²¨!`;
}
ν면ꡬν