vendredi 11 novembre 2016

Rolling 2 dice randomly (with URLs)

I have this problem with rolling 2 dice, and then, if they match, the script should return which pair it was.

I have the dice pics on my server.

I must be doing some very simple things wrong but can't now figure out what the are.

Here is the URL of to this tasks page that I've made: http://ift.tt/2eYXaYu

And here is my current code on that page:

<!DOCTYPE html>
<html>

<head>
  <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
  <meta charset="utf-8">
  <title>ot4b</title>
  <link href="ot4.css" rel="stylesheet" type="text/css">
</head>

<body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <button type="button" id="button" onClick="pelaa()">Pelaa</button>
  <p>
    <img src="noppakuvat/noppa1.jpg" id="noppa1" />
    <img src="noppakuvat/noppa1.jpg" id="noppa2" />
  </p>

  <p id="pari"></p>
  <script>
    var n1 = document.getElementsById("noppa1");
    var n2 = document.getElementsById("noppa2");
    var pari = document.getElementById("pari");
    var pairs = ["Ykköset", "Kakkoset", "Kolmoset", "Neloset", "Vitoset", "Kutoset"];

    function pelaa() {
      var dice1 = Math.floor(Math.random() * (6)) + 1;
      var dice2 = Math.floor(Math.random() * (6)) + 1;
      var url1 = "noppakuvat/noppa" + dice1 + ".jpg";
      var url2 = "noppakuvat/noppa" + dice2 + ".jpg";

      n1.src = url1;
      n2.src = url2;

      if dice1 == dice2 {
        pari.innerHTML = pairs.[dice1 - 1];
      } else pari.innerHTML = "";
    }
  </script>
</body>
</html>



Aucun commentaire:

Enregistrer un commentaire