<?php
/**
* Fungsi tekaTekiTeko - Variasi permainan FizzBuzz dengan aturan khusus
*
* @param int $batas - Batas atas untuk mencetak angka (minimal 20)
* @throws InvalidArgumentException - Jika parameter tidak sesuai kriteria
*/
function tekaTekiTeko(int $batas): void
{
// Validasi parameter
if ($batas < 0) {
throw new InvalidArgumentException("Parameter harus berupa unsigned integer (tidak boleh negatif)");
}
if ($batas < 20) {
throw new InvalidArgumentException("Parameter harus memiliki nilai minimal 20");
}
// Loop dari 1 sampai batas
for ($i = 1; $i <= $batas; $i++) {
$output = "";
// Cek apakah habis dibagi 2
if ($i % 2 == 0) {
$output .= "Teka";
}
// Cek apakah habis dibagi 3
if ($i % 3 == 0) {
$output .= "Teki";
}
// Cek apakah habis dibagi 5
if ($i % 5 == 0) {
$output .= "Teko";
}
// Jika tidak ada yang habis dibagi, cetak angka asli
if ($output == "") {
$output = $i;
}
echo $output . "\n";
}
}
// Contoh penggunaan
try {
echo "=== Contoh penggunaan tekaTekiTeko(30) ===\n";
tekaTekiTeko(30);
echo "\n=== Test dengan parameter tidak valid ===\n";
// Test dengan nilai kurang dari 20
try {
tekaTekiTeko(15);
} catch (InvalidArgumentException $e) {
echo "Error: " . $e->getMessage() . "\n";
}
// Test dengan nilai negatif
try {
tekaTekiTeko(-5);
} catch (InvalidArgumentException $e) {
echo "Error: " . $e->getMessage() . "\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
?>