1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

✅ 今日学んだことリスト

  • filter()で配列から偶数だけ取り出す方法
  • setInterval()clearInterval()でカウントダウン
  • Math.random()を使って背景色をランダムに変更
  • Math.max(...array) で配列の中の最大値を取得
  • Dateオブジェクトで現在の日時を整形表示(padStart()も使用)

偶数だけを取り出す関数

const pickEven = arr => arr.filter(num => num % 2 === 0);

1秒ごとにカウントダウン (5 → 0)

let count = 5;
const timerId = setInterval(() => {
  console.log(count);
  if (count === 0) clearInterval(timerId);
  count--;
}, 1000);

クリックで背景色がランダムに変わる

button.addEventListener("click", function () {
  const r = Math.floor(Math.random() * 256);
  const g = Math.floor(Math.random() * 256);
  const b = Math.floor(Math.random() * 256);
  document.body.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
});

配列の最大値を取得する

const numbers = [1, 2, 3, 9, 0];
console.log(Math.max(...numbers)); // 9

現在の日時を「YYYY/MM/DD HH:MM:SS」で表示

const now = new Date();
const formatted = `${now.getFullYear()}/$ {
  String(now.getMonth() + 1).padStart(2, "0")
}/${String(now.getDate()).padStart(2, "0`)} ${
  String(now.getHours()).padStart(2, "0")
}:${String(now.getMinutes()).padStart(2, "0`)}:${String(now.getSeconds()).padStart(2, "0")}`;
console.log(formatted);

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?