Gotenks
Gotenks

Reputation: 5

Image not randomising in array?

function doSingle()
{
    var luck= ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
    var imagesArray = ["thumbs/lr/lrgokublack.png", "thumbs/lr/lrtrunks.png", "thumbs/lr/lrgohan.png", "thumbs/lr/lrmajinvegeta.png"];
    var ssrArray = ["thumbs/ssr/beerus1.png", "thumbs/ssr/broly1.png"];
    lucknumber = Math.floor((Math.random() * luck.length));

    if (lucknumber < 8) {
        function displayImage() {
        var num = Math.floor(Math.random() * ssrArray.length);
        window.canvas.src = ssrArray[num];
}
    } else {
        function displayImage() {
        var num = Math.floor(Math.random() * imagesArray.length);
        window.canvas.src = imagesArray[num];
        }
}
}

This code will not work, but I think it's correct? I'm pretty sure I called the doSingle too in my html code;

<head>
<p id="soup"></p>
<link rel="stylesheet" type="text/css" href="formalign.css">
      <script type="text/javascript" src="trinit5.js"></script>
      <input onclick="doSingle();" id="image" type="image" alt="Summon" src="ssbutton1.png" width="25%"/> 
      <img id="canvas"></img>
      <div class="element"></div>

</head>

My intentions are to randomise the luck, and depending on that luck, a certain number will give you an SR or a SSR (rarities) Also, a few people said this html code is wrong, but it works for me, and won't work when I change it, so I'll leave it as it is :D Thanks in advance!

EDIT: In inspect element, is says doSingle() is not defined????

Upvotes: 0

Views: 30

Answers (1)

Ele
Ele

Reputation: 33726

You have two issues:

You're declaring variables inline, so you don't need multiple usages of keyword var

var luck= ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
   var imagesArray = ["thumbs/lr/lrgokublack.
   ^

You're not calling the function displayImage, you just declared it. So, call it.

function displayImage() {
^

This code snippet has those fixes:

function doSingle() {
  var luck = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
    imagesArray = ["thumbs/lr/lrgokublack.png", "thumbs/lr/lrtrunks.png", "thumbs/lr/lrgohan.png", "thumbs/lr/lrmajinvegeta.png"];
  var ssrArray = ["thumbs/ssr/beerus1.png", "thumbs/ssr/broly1.png"];
  lucknumber = Math.floor((Math.random() * luck.length));

  if (lucknumber < 8) {
    function displayImage() {
      var num = Math.floor(Math.random() * ssrArray.length);
      window.canvas.src = ssrArray[num];
    }

    displayImage();
  } else {
    function displayImage() {
      var num = Math.floor(Math.random() * imagesArray.length);
      window.canvas.src = imagesArray[num];
    };

    displayImage();
  }
}

doSingle();
<p id="soup"></p>
<link rel="stylesheet" type="text/css" href="formalign.css">
<script type="text/javascript" src="trinit5.js"></script>
<input onclick="doSingle();" id="image" type="image" alt="Summon" src="ssbutton1.png" width="25%" />
<img id="canvas"></img>
<div class="element"></div>

Upvotes: 1

Related Questions