Cấp độ: elementary 30 phútmin Miễn phí Free Junior Builder Junior Builder

JavaScript: if/else — Game đoán số JavaScript: if/else — Number guessing game

Game đoán số ngẫu nhiên A random number guessing game

Bạn sẽ học gì What you will learn

  • Viết câu lệnh if/else/else if Write if/else/else if statements
  • Nhận input từ người dùng Get user input
  • Tạo số ngẫu nhiên với Math.random() Generate random numbers with Math.random()

Bạn sẽ tạo gì What you will build

Game đoán số ngẫu nhiên A random number guessing game

Code mẫu Sample code

javascript
const bíMật = Math.floor(Math.random() * 10) + 1; // 1-10
const đoán = parseInt(prompt("Đoán số từ 1-10 / Guess 1-10:"));

if (đoán === bíMật) {
  alert("🎉 Chính xác! / Correct!");
} else if (đoán > bíMật) {
  alert("📉 Quá cao! / Too high!");
} else {
  alert("📈 Quá thấp! / Too low!");
}