✅ 今日学んだことリスト
-
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);