Skip to content

Instantly share code, notes, and snippets.

@selam
Created July 5, 2015 19:38
Show Gist options
  • Save selam/2278647829ede7c5814e to your computer and use it in GitHub Desktop.
Save selam/2278647829ede7c5814e to your computer and use it in GitHub Desktop.
javascript random answer
<html>
<head>
// bootstrap ve jquery bağlantıları buraya gelecek
</head>
<body>
<script>
// cevap listemizi oluşturalım, her satırda bir cevap
var cevap_listesi = [
'Bir cevap',
'başka bir cevap',
'bambaşka bir cevap',
'hadi canım?',
'ciddi misin?',
'nasip kısmet',
'maşallah demek lazım',
'inşallah da diyelim',
'cümlemize, amin!',
'goygoy zamanı!'
];
var cevap_sayisi = cevap_listesi.length;
</script>
<!-- butona tıklandığında rastgele bir cevap seçilerek aşağıdaki div'e yerleştirilecek. -->
<input button="Söyle bana" onclick="javascript:cevapla();return false;">
<div id="cevabin_goruntulenecegi_alan"></div>
<script>
/*
* cevapla isimli javascript fonksiyonu, cevap listesinden rastgele bir cevap seçer ve div içine yerleştirir
*/
function cevapla() {
// cevap seçelim
var cevap = cevap_listesi[Math.floor(Math.random()*cevap_sayisi)];
// html içine cevabimizi yerleştirelim.
$("cevabin_goruntulenecegi_alan").text(cevap);
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment