GiantStepDEV
article thumbnail

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}๋ฒˆ ๋‹น์ฒจ!`;
}

ํ™”๋ฉด๊ตฌํ˜„

profile

GiantStepDEV

@kongmi

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!