Newer
Older
hello-programmer-world / src / sample / js / work / price-calculator.html
@h.sakamoto h.sakamoto 14 days ago 1 KB work js
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>商品の合計金額計算</title>
  <style>
    body {
      font-family: sans-serif;
      padding: 20px;
    }
    .container {
      max-width: 400px;
      margin: 0 auto;
    }
    .item-list {
      background-color: #f5f5f5;
      padding: 15px;
      border-radius: 4px;
      margin: 15px 0;
    }
    .item {
      padding: 5px 0;
    }
    button {
      padding: 10px 20px;
      font-size: 16px;
      cursor: pointer;
      background-color: #2196F3;
      color: white;
      border: none;
      border-radius: 4px;
      margin-top: 10px;
    }
    button:hover {
      background-color: #0b7dda;
    }
    #result {
      margin-top: 20px;
      padding: 15px;
      background-color: #e7f3ff;
      border-left: 4px solid #2196F3;
      font-size: 20px;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>商品の合計金額計算</h1>
    <div class="item-list">
      <div class="item">りんご: 120円</div>
      <div class="item">バナナ: 80円</div>
      <div class="item">みかん: 100円</div>
    </div>
    <button id="calculateButton">合計を計算する</button>
    <div id="result"></div>
  </div>

  <script>
    const calculateButton = document.querySelector("#calculateButton");
    const result = document.querySelector("#result");

    calculateButton.addEventListener("click", () => {
      const prices = [120, 80, 100];
      
      let total = 0;
      for (const price of prices) {
        total = total + price;
      }

      result.textContent = `合計金額: ${total}円`;
    });
  </script>
</body>
</html>