Random Words Generator Javascript

Click the button to generate a random (word) password.
Generated:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Password (Word) Generator</title>
<style>
#copyButton {
cursor: pointer;
margin-left: 10px;
font-size: 20px;
background: none;
border: none; }
</style>
</head>
<body>
<h1>Random Password (Word) Generator</h1>
<p>Click the button to generate a random password.</p>
<button id="generate">Generate</button><br><br>
Generated: <input id="hasil" type="text" style="width:70%" readonly><button id="copyButton" title="Copy to clipboard">📋</button>
<script>
const listkata1 = ["Abaikan", "Hancurkan", "Musnahkan", "Hilangkan", "Bantinglah", "Bakarlah", "Biarkan", "Tendanglah", "Belilah", "Buanglah", "Kuburlah", "Lemparlah", "Bungkuslah", "Simpanlah", "Sembunyikan", "Pukullah", "Injaklah", "Singkirkan"];
const listkata2 = ["Celana", "Kaos", "Kertas", "Baskom", "Koran", "Sendok", "Botol", "Piring", "Panci", "Kasur", "Bantal", "Guling", "Kursi", "Sarung", "Selimut", "Meja", "Karung", "Keran", "Kantong", "Kardus", "Jaket", "Tas", "Kresek"];
const listkata3 = ["Rusak", "Usang", "Berjamur", "Kumal", "Burik", "Culun", "Koplak", "Jelek", "Kotor", "Miring", "Somplak", "Bolong", "Bau", "Norak", "Cupu"];
const listkata4 = ["Merah", "Kuning", "Hijau", "Biru", "Ungu", "Magenta", "Bluwek", "Banget", "Dikit", "Sangat", "Setengah", "Separuh", "Semua", "Sebagian", "Sepotong", "Segala", "Secuil"];
function getRandomElement(arr) {
const randomIndex = Math.floor(Math.random() * arr.length);
return arr[randomIndex];
}
function generateRandomPassword() {
const kata1 = getRandomElement(listkata1);
const kata2 = getRandomElement(listkata2);
const kata3 = getRandomElement(listkata3);
const kata4 = getRandomElement(listkata4);
const password = kata1 + kata2 + kata3 + kata4;
return password;
}
document.getElementById("generate").addEventListener("click", function () {
const password = generateRandomPassword();
document.getElementById("hasil").value = password;
});
document.getElementById("copyButton").addEventListener("click", function () {
const hasilField = document.getElementById("hasil");
hasilField.select();
hasilField.setSelectionRange(0, 99999);
document.execCommand("copy");
alert("Hasil sudah tersalin di clipboard!");
});
</script>
</body>
</html>
Komentar
Posting Komentar