Ghi chép buổi 29/10/2025

console.log("Hello Word")

Đây là loạt bài tập luyện tư duy xử lý chuỗi và kiểm tra dữ liệu đầu vào. Quan trọng: cách tách logic rõ ràng, dễ tái sử dụng.

Bài 1. Tô sáng từ khóa & đếm số lần xuất hiện

Yêu cầu: Cho trước một chuỗi nội dung và một từ khóa. - Bôi vàng (highlight) tất cả vị trí xuất hiện của từ khóa trong đoạn văn. - Đếm tổng số lần xuất hiện. - Hiển thị lại nội dung sau khi đã highlight.

Code của bài 1

console.log("Hello Word");

// 29-10-2025 (Làm bài tập)
// Bài 1:
let keyWord = "o";
let total = 0;

let content = \`Ipsum dolor, sit amet consectetur Lorem  adipisicing elit. Minus velit id
    recusandae iusto autem sunt ipsum, temporibus eius veritatis, minima
    officiis sint quidem explicabo impedit mollitia quaerat aut ducimus tempora
    Lorem eligendi. Possimus tempora exercitationem, assumenda neque, ipsam
    cupiditate hic dolores aliquam accusantium Lorem quidem corrupti quisquam
    voluptate,  Lorem  odio?Lorem ipsum dolor sit amet consectetur adipisicing
    elit. Facilis, exercitationem? Modi, excepturi corrupti reiciendis, quae
    itaque veniam accusamus dolorem Lorem quo tempora dolorum eveniet officiis
    nihil veritatis iste libero odit, nesciunt Lorem. Esse eum eveniet a. In,
    distinctio Lorem non provident cum magni eos aut odit fuga facere
    ratione eius!\`;

let newContent = "";
let position = content.toLowerCase().indexOf(keyWord.toLowerCase());

while (position !== -1) {
  newContent =
    newContent +
    content.slice(0, position) +
    "<span>" +
    content.slice(position, position + keyWord.length) +
    "</span>";

  // Cắt bỏ phần đã xử lý khỏi content
  content = content.slice(position + keyWord.length);

  total++;

  // Tìm tiếp
  position = content.toLowerCase().indexOf(keyWord.toLowerCase());
}

// Ghép phần còn lại
newContent += content;

document.body.innerHTML = \`<p>Từ khóa : \${keyWord}</p>
<p>\${newContent}</p>
<p>Đã tìm thấy \${total} kết quả phù hợp.</p>\`;

Giải thích ý tưởng

  • Dùng indexOf để tìm vị trí (position) xuất hiện tiếp theo của từ khóa.
  • Mỗi lần tìm thấy, ta cắt phần trước và phần trúng keyword ra và bọc bằng thẻ <span>...</span> để highlight.
  • Sau đó cập nhật lại biến content chỉ còn phần CHƯA xử lý.
  • Tiếp tục lặp với while cho đến khi không còn xuất hiện nào nữa (position === -1).
  • Cuối cùng ghép phần còn lại vào newContent và in ra.

Kết quả hiển thị mẫu

Từ khóa : o

Ipsum dolor, sit amet consectetur Lorem  adipisicing elit...

Đã tìm thấy: total lần xuất hiện.

Đây chính là nền bài "highlight text" mà bạn đã học trước đó: tìm kiếm từ khóa trong đoạn văn và bọc nó bằng <span> để đổi màu nền.

Bài 2. Kiểm tra độ mạnh của mật khẩu

Yêu cầu: Viết hàm isStrongPassword(password) để kiểm tra chất lượng mật khẩu. Trả về thông báo lỗi cụ thể nếu chưa đạt yêu cầu.

Điều kiện mật khẩu mạnh

  • Dài tối thiểu 8 ký tự.
  • Có ít nhất 2 ký tự in hoa (A-Z).
  • Có ít nhất 2 ký tự thường (a-z).
  • Có ít nhất 1 chữ số (0-9).
  • Có ít nhất 1 ký tự đặc biệt trong !@#$%^&*().

Code của bài 2

// Bài 2: Kiểm tra độ mạnh yếu của mật khẩu.
// Điều kiện:
// - Từ 8 kí tự trở lên
// - Có ít nhất 2 kí tự viết hoa
// - Có ít nhất 2 kí tự viết thường
// - Có ít nhất 1 số
// - Có ít nhất 1 kí tự đặc biệt

function isStrongPassword(password) {
  const specialChar = "!@#$%^&*()";
  const number = "0123456789";

  if (password.length < 8)
    return "Mật khẩu cần ít nhất 8 kí tự.";

  let hasSpecial = false;
  let hasNum = false;
  let hasLowChar = false;
  let hasUpChar = false;
  let countUpChar = 0;
  let countLowChar = 0;

  for (let i = 0; i < password.length; i++) {
    if (specialChar.includes(password[i])) {
      hasSpecial = true;
    } else if (number.includes(password[i])) {
      hasNum = true;
    } else if (password[i] >= "A" && password[i] <= "Z") {
      countUpChar += 1;
    } else if (password[i] >= "a" && password[i] <= "z") {
      countLowChar += 1;
    }
  }

  if (countUpChar > 1) {
    hasUpChar = true;
  }

  if (countLowChar > 1) {
    hasLowChar = true;
  }

  if (!hasSpecial) {
    return "Mật khẩu cần ít nhất 1 kí tự đặc biệt.";
  }

  if (!hasNum) {
    return "Mật khẩu cần ít nhất 1 kí tự số.";
  }

  if (!hasUpChar) {
    return "Mật khẩu cần ít nhất 2 kí tự viết hoa.";
  }

  if (!hasLowChar) {
    return "Mật khẩu cần ít nhất 2 kí tự viết thường";
  }

  return \`Mật khẩu của bạn là : \${password}\`;
}

console.log(isStrongPassword("PP1@nn1aaaa"));

Phân tích logic duyệt từng ký tự

  • specialChar.includes(password[i]) → phát hiện ký tự đặc biệt.
  • number.includes(password[i]) → phát hiện chữ số.
  • password[i] >= "A" && password[i] <= "Z" → ký tự in hoa (A-Z).
  • password[i] >= "a" && password[i] <= "z" → ký tự thường (a-z).
  • Dùng biến đếm countUpChar, countLowChar rồi chuyển thành cờ hasUpChar, hasLowChar để kiểm tra điều kiện tối thiểu.

Ưu điểm: Bạn đang trả về thông báo cụ thể vì sao mật khẩu chưa đạt. Đây là UX rất tốt cho form đăng ký thực tế.