🎨 Frontend/JavaScript

🍦Vanilla JS β‘€ 객체 - (2).4 ν‘œμ€€ λ‚΄μž₯ 객체 : Map, Math, 랜덀 예제

kongmi 2023. 3. 29. 17:55

Map

  • Map은 μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ μ œκ³΅ν•˜λŠ” 데이터 ꡬ쑰 쀑 ν•˜λ‚˜λ‘œ, key - value 쌍으둜 데이터λ₯Ό μ €μž₯ν•˜κ³  κ΄€λ¦¬ν•©λ‹ˆλ‹€.
  • Map은 은 객체와 μœ μ‚¬ν•˜μ§€λ§Œ, κ°μ²΄μ™€λŠ” λ‹€λ₯΄κ²Œ key에 λ‹€μ–‘ν•œ μžλ£Œν˜•μ„ μ‚¬μš©ν•  수 있으며, μ›μ‹œ μžλ£Œν˜•λ„ key에 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  1. set(key, value) : Map은 객체에 key - value μŒμ„ μΆ”κ°€ν•©λ‹ˆλ‹€.
  2. get(key) : Map은 κ°μ²΄μ—μ„œ μ§€μ •ν•œ key에 λŒ€ν•œ 값을 λ°˜ν™˜ν•©λ‹ˆλ‹€.
  3. has(key) : Map은 κ°μ²΄μ—μ„œ μ§€μ •ν•œ keyκ°€ μ‘΄μž¬ν•˜λŠ”μ§€ μ—¬λΆ€λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
  4. delete(key) : Map은 κ°μ²΄μ—μ„œ μ§€μ •ν•œ key에 λŒ€ν•œ key - value μŒμ„ μ œκ±°ν•©λ‹ˆλ‹€.
  5. clear() : Map은 κ°μ²΄μ—μ„œ λͺ¨λ“  key- value μŒμ„ μ œκ±°ν•©λ‹ˆλ‹€.
  6. 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 객체 ( μžλ°”λž‘ λ˜‘κ°™μŒ )
  1. Math.abs(x) : x의 μ ˆλŒ€κ°’ λ°˜ν™˜
  2. Math.ceil(x) : x μ΄μƒμ˜ κ°€μž₯ μž‘μ€ μ •μˆ˜ λ°˜ν™˜
  3. Math.floor(x) : x μ΄ν•˜μ˜ κ°€μž₯ 큰 μ •μˆ˜ λ°˜ν™˜
  4. Math.round(x) : xλ₯Ό λ°˜μ˜¬λ¦Όν•œ μ •μˆ˜ λ°˜ν™˜
  5. Math.max(x1, x2, ... , xn) : μ£Όμ–΄μ§„ κ°’λ“€ 쀑 κ°€μž₯ 큰 κ°’ λ°˜ν™˜
  6. Math.min(x1, x2, ... , xn) : μ£Όμ–΄μ§„ κ°’λ“€ 쀑 κ°€μž₯ μž‘μ€ κ°’ λ°˜ν™˜
  7. Math.pow(x, y) : x의 y 제곱 λ°˜ν™˜
  8. Math.sqrt(x) : x의 제곱근 λ°˜ν™˜
  9. 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}번 당첨!`;
}

ν™”λ©΄κ΅¬ν˜„