dimanche 27 novembre 2016

Little mistake somewhere. Help needed

I wrote a little browser-game.

The rules are easy: you have 15 seconds to decide if you know the name written on the screen.

You have two buttons: "i know"/"give up" - depends on what you want to choose.

If you choose "give up" (or time ends) photo 1 appears. Otherwise, photo 2 will be shown.

Whole operation is looped.

Here's my question: I wanted to choose random name from array "word" every single round, so I wrote function "random_word()". I put it into "timer()", "surrender()" and "winning()" functions. But it doesn't work.

I'm just starting with programming, so I'll be grateful for possibly easiest to understand code. Thanks for all help.

Here's my code:

<!DOCTYPE html>
<html>
<head>
<style>

</style>
<script src="/scripts/snippet-javascript-console.min.js?v=1"></script>
</head>
<body> 
<button id= "btnSurrender">give up</button>

<button id= "btnWinning">i know</button>

<p id="seconds">15</p>
<div id="photo"></div>
<script type="text/javascript">


var word = new Array(3);
word[0] = "Michael";
word[1] = "Simon";
word[2] = "Peter";
word[3] = "Mark";

function random_word(){
var randomWord = word[Math.floor(Math.random()*word.length)]
}




var btn1 = document.getElementById("btnSurrender");
var btn2 = document.getElementById("btnWinning");
var pic = document.getElementById("photo");
var secondsP = document.getElementById('seconds');

var clock = null;


btn1.addEventListener("click", surrender);
btn2.addEventListener("click", winning);

function timer () {

random_word();

clearInterval(clock);

var start = new Date().getTime();

clock = setInterval(function() {



         pic.innerHTML='<img src="" alt="">';
         var seconds = Math.round(15 - (new Date().getTime() - start) / 1000);


         if (seconds >= 0) {
           secondsP.textContent = seconds;
         } else {
           clearInterval(clock);
         }

         if (seconds === 0) {
           pic.innerHTML='<img src="mops bops.png" alt="">';

         }
}, 1000);
}

function surrender(){

clearInterval(clock);
pic.innerHTML='<img src="mops bops.png" alt="">';
secondsP.textContent = 0;
setTimeout(timer,2000);
word[Math.floor(Math.random()*word.length)]
random_word();
}
function winning(){

clearInterval(clock);
pic.innerHTML='<img src="mopsik.jpg" alt="">';
secondsP.textContent = 0;
setTimeout(timer,2000);
word[Math.floor(Math.random()*word.length)]
random_word();
}

timer();

document.write(randomWord)
setInterval(timer, 17000)

</script>
</body>
</html>




Aucun commentaire:

Enregistrer un commentaire