JavaScript数组无法正常工作

时间:2016-05-05 05:40:31

标签: javascript

我正在为我的网站寻求帮助。我正在使用数组为我的图像保存不同的类别,但是当我加载我的网站时,它没有显示它出现断开链接的图像。我怎样才能解决这个问题?网站http://tacticalcards.esy.es/

HTML

            <button class="open" onclick="pack100()">Open &#163;250 Cards.</button>
            <br />
            <div class=card-group>
                <img class="card" src="http://tacticalcards.esy.es/images/blank.png" id="cp1" />
                <p>Worth: &#163;<span id="w1">0</span></p>
            </div>

JAVASCRIPT

var bronze = "http://tacticalcards.esy.es/images/a.k.a.bronze.jpg";
var silver = new Array("http://tacticalcards.esy.es/images/ssgsilver.jpg","http://tacticalcards.esy.es/images/tacticalknife.silver.jpg","http://tacticalcards.esy.es/images/clockpistol.silver.jpg");
var gold = new Array("http://tacticalcards.esy.es/images/a.w.p.gold.jpg","http://tacticalcards.esy.es/images/m4ai-s.gold.jpg");
var legends = "http://tacticalcards.esy.es/images/karambitee.jpg";
var cards = new Array(bronze, silver, gold, legends);
function pack100(){
var c1 = Math.floor(Math.random() * cards.length);
document.getElementById("cp1").src = cards[c1];
var cp1 = document.getElementById('cp1').src;
if (cp1 == bronze){
        profit += 25;
        document.getElementById("w1").innerHTML = 25;
        document.getElementById("profit").innerHTML = profit;
    };
    if (cp1  silver){
        profit +=50 ;
        document.getElementById("w1").innerHTML = 50;
        document.getElementById("profit").innerHTML = profit;
    };
    if (cp1 == gold){
        profit += 100;
        document.getElementById("w1").innerHTML = 100;
        document.getElementById("profit").innerHTML = profit;
    };
    if (cp1 == legends){
        profit += 250;
        document.getElementById("w1").innerHTML = 250;
        document.getElementById("profit").innerHTML = profit;
    };
};

2 个答案:

答案 0 :(得分:2)

保留一个objects数组,每个对象都有一个键来区分源。另请注意,您需要找到2个随机数,第一个用于获取随机数object,第二个用于从源数组中查找random-index。在我们处理数组的索引时,您必须拥有array中的所有图像源。

var bronze = ["http://tacticalcards.esy.es/images/a.k.a.bronze.jpg"];
var silver = ["http://tacticalcards.esy.es/images/ssgsilver.jpg", "http://tacticalcards.esy.es/images/tacticalknife.silver.jpg", "http://tacticalcards.esy.es/images/clockpistol.silver.jpg"];
var gold = ["http://tacticalcards.esy.es/images/a.w.p.gold.jpg", "http://tacticalcards.esy.es/images/m4ai-s.gold.jpg"];
var legends = ["http://tacticalcards.esy.es/images/karambitee.jpg"];

var cards = [{
  key: 'bronze',
  data: bronze
}, {
  key: 'silver',
  data: silver
}, {
  key: 'gold',
  data: gold
}, {
  key: 'legends',
  data: legends
}];

function pack100() {
  var profit = 0;
  var c1 = Math.floor(Math.random() * cards.length);
  var rand = Math.floor(Math.random() * cards[c1].data.length);
  document.getElementById("cp1").src = cards[c1].data[rand];
  var cp1 = cards[c1].key;
  if (cp1 == 'bronze') {
    profit += 25;
    document.getElementById("w1").innerHTML = 25;
    document.getElementById("profit").innerHTML = profit;
  };
  if (cp1 == 'silver') {
    profit += 50;
    document.getElementById("w1").innerHTML = 50;
    document.getElementById("profit").innerHTML = profit;
  };
  if (cp1 == 'gold') {
    profit += 100;
    document.getElementById("w1").innerHTML = 100;
    document.getElementById("profit").innerHTML = profit;
  };
  if (cp1 == 'legends') {
    profit += 250;
    document.getElementById("w1").innerHTML = 250;
    document.getElementById("profit").innerHTML = profit;
  };
};
<button class="open" onclick="pack100()">Open &#163;250 Cards.</button>
<br />
<div class=card-group>
  <img class="card" src="http://tacticalcards.esy.es/images/blank.png" id="cp1" />
  <p>Worth: &#163;<span id="w1">0</span>
  </p>
  <div id="profit"></div>
</div>

Fiddle Demo

答案 1 :(得分:0)

你必须使用随机的二维数组,在此之前你必须找到二维数组的长度。并将其设置为另一个数组以获取随机2d数组。 Ex [0] [1]或[1] [0]或p [3] [1],您还必须将所有这些转换为正确的2d数组
我试过并获得成功。

var bronze =new Array( "http://tacticalcards.esy.es/images/a.k.a.bronze.jpg");
                var silver = new Array("http://tacticalcards.esy.es/images/ssgsilver.jpg","http://tacticalcards.esy.es/images/tacticalknife.silver.jpg","http://tacticalcards.esy.es/images/clockpistol.silver.jpg");
                var gold = new Array("http://tacticalcards.esy.es/images/a.w.p.gold.jpg","http://tacticalcards.esy.es/images/m4ai-s.gold.jpg");
                var legends = new Array("http://tacticalcards.esy.es/images/karambitee.jpg");
                var cards = new Array(bronze, silver, gold, legends);

                var arraycount=[cards[0].length,cards[1].length,cards[2].length,cards[3].length]
                function pack100(){
                var array0=Math.floor(Math.random() *arraycount.length) ;
                var c1 = Math.floor(Math.random() * arraycount[array0]);
                document.getElementById("cp1").src = cards[array0][c1];

                var cp1 = document.getElementById('cp1').src;
                if (cp1 == bronze){
                        profit += 25;
                        document.getElementById("w1").innerHTML = 25;
                        document.getElementById("profit").innerHTML = profit;
                    };
                    if (cp1==silver){
                        profit +=50 ;
                        document.getElementById("w1").innerHTML = 50;
                        document.getElementById("profit").innerHTML = profit;
                    };
                    if (cp1 == gold){
                        profit += 100;
                        document.getElementById("w1").innerHTML = 100;
                        document.getElementById("profit").innerHTML = profit;
                    };
                    if (cp1 == legends){
                        profit += 250;
                        document.getElementById("w1").innerHTML = 250;
                        document.getElementById("profit").innerHTML = profit;
                    };
            }