如何比较数组中的元素(javascript)

时间:2013-11-16 16:04:50

标签: javascript arrays compare

道歉,如果这听起来非常基本,但在编码方面我是一个新手(尽管我正努力做得更好!)

我正在使用javascript创建一个简单的更高或更低的纸牌游戏,其中用户猜测下一张牌是否会高于或低于显示的当前牌。

我目前拥有52张卡片和一组卡片。我正在使用Math.random从数组中随机生成一张卡片。

我想要的是比较两张牌的价值,但我不知道如何去做。我有一种感觉可能涉及IndexOf,但我不确定。

万分感谢如果这不是主题或重复,我很抱歉!

如果有帮助,这是我的数组。

function randomCard()
{
var cardArray = ["Club0.jpg","Club1.jpg","Club3.jpg","Club3.jpg","Club4.jpg","Club5.jpg","Club6.jpg","Club7.jpg","Club8.jpg","Club9.jpg","Club10.jpg","Club11.jpg","Club12.jpg","Diamond0.jpg","Diamond1.jpg","Diamond2.jpg","Diamond3.jpg","Diamond4.jpg","Diamond5.jpg","Diamond6.jpg","Diamond7.jpg","Diamond8.jpg","Diamond9.jpg","Diamond10.jpg","Diamond11.jpg","Diamond12.jpg","Heart0.jpg","Heart1.jpg","Heart2.jpg","Heart3.jpg","Heart4.jpg","Heart5.jpg","Heart6.jpg","Heart7.jpg","Heart8.jpg","Heart9.jpg","Heart10.jpg","Heart11.jpg","Heart12.jpg","Spade0.jpg","Spade1.jpg","Spade2.jpg","Spade3.jpg","Spade4.jpg","Spade5.jpg","Spade6.jpg","Spade7.jpg","Spade8.jpg","Spade9.jpg","Spade10.jpg","Spade11.jpg","Spade12.jpg"];

var pickCard = cardArray[Math.floor(Math.random() * cardArray.length)];
document.getElementById("card").src = pickCard;
}

3 个答案:

答案 0 :(得分:4)

我认为这里的问题并不是你不了解数组和循环,而是如何在开始之前正确构建问题。目前您有以下内容:

var cardArray = ["Club0.jpg","Club1.jpg","Club3.jpg"...];

而且你让自己的生活变得更加困难,因为对这些数组元素进行数值比较 - 虽然并非不可能 - 会涉及正则表达式测试,你可能还没有做好准备。

如果相反你说“我有4套西装,卡片的价值为1 - 13(11个是杰克,最多13个国王,Aces低),那么你可以开始更好地理解问题范围

让我们从制作套牌开始 - 四套西服,每套14张。

var deck = {
  heart: [1,2,3,4,5,6,7,8,9,10,11,12,13],
  club: [1,2,3,4,5,6,7,8,9,10,11,12,13],
  spade: [1,2,3,4,5,6,7,8,9,10,11,12,13],
  diamond: [1,2,3,4,5,6,7,8,9,10,11,12,13]
};

我们需要在一个单独的数组中保存套装的名称,以便我们可以访问该对象。

var suits = ['heart','club','spade','diamond'];

接下来,memo很重要。由于我们不想在每次抽奖时使用相同的卡,我们需要记下已经拍摄的卡片; memo是我们参考的精选卡片商店。

var memo = [];

现在为函数drawRandomCard。它需要我们的西装阵列中的随机套装,以及来自所选套装阵列的数字。如果该卡位于memo,我们会再次绘制,否则我们会将其添加到memo并返回该卡。但是,因为我们仍然希望对值进行比较,所以我们将返回一个数组,第一个元素是套装,第二个元素是值。

function drawRandomCard() {
  var suit = suits[Math.floor(Math.random() * suits.length)];
  var number = deck[suit][Math.floor(Math.random() * 13)];
  if (memo[suit + number]) drawRandomCard();
  memo.push(suit + number)
  return [suit, number];
}

例如:

var card1 = drawRandomCard();
var card2 = drawRandomCard();

要测试这些值,请将数组的第二个值相互比较:

console.log(card1[1] > card2[1]); // true or false

然后,如果您愿意,可以使用join将这些变量的值与您需要加载的任何图像相匹配,以将卡阵列元素合并在一起。

var img = new Image();
img.src = card1.join('') + '.jpg'; // eg. diamond4.jpg

或者:

document.getElementById("card").src = card1.join('') + '.jpg';

Fiddle

答案 1 :(得分:0)

我会使用类似的东西:

function Card(suit, number) {
    this.suit = suit;
    this.number = number;
}
Card.prototype.image = function() {
    return this.suit + this.number + '.jpg';
};
Card.compare = function(a, b) {
    /* Define how you want to compare cards. My guess is: */
    return a.number < b.number;
};
Card.prototype.compareTo = function(other) {
    return Card.compare(this, other);
};

var suits = ['Club', 'Diamond', 'Heart', 'Spade'],
    cardArray = [];
for (var i = 0; i < suits.length; ++i) {
    for (var j = 0; j <= 12; ++j) cardArray.push(new Card(suits[i], j));
}
var currentCard = cardArray[0]; /* Or whatever initial card */
function randomCard() {
    var pickCard = cardArray[Math.random() * cardArray.length | 0];
    if(currentCard.compareTo(pickCard)) {
        /* Do something */
    } else {
        /* Do something else */
    }
    document.getElementById("card").src = pickCard.image();
    currentCard = pickCard;
}

如果您想要一旦卡被挑选,就不能再次挑选(如@MackieeE所说),替换

var pickCard = cardArray[Math.random() * cardArray.length | 0];

var pickCard = cardArray.splice(Math.random() * cardArray.length | 0, 1)[0];

要从cardArray中删除已选择的卡片。

答案 2 :(得分:0)

如果card="Club10.jpg"

,这将从卡片的图片字符串中获取值
card.match(/\d+/g)

将返回:

10

例如:

var cardArray = ["Club0.jpg","Club1.jpg","Club3.jpg","Club3.jpg","Club4.jpg","Club5.jpg","Club6.jpg","Club7.jpg","Club8.jpg","Club9.jpg","Club10.jpg","Club11.jpg","Club12.jpg","Diamond0.jpg","Diamond1.jpg","Diamond2.jpg","Diamond3.jpg","Diamond4.jpg","Diamond5.jpg","Diamond6.jpg","Diamond7.jpg","Diamond8.jpg","Diamond9.jpg","Diamond10.jpg","Diamond11.jpg","Diamond12.jpg","Heart0.jpg","Heart1.jpg","Heart2.jpg","Heart3.jpg","Heart4.jpg","Heart5.jpg","Heart6.jpg","Heart7.jpg","Heart8.jpg","Heart9.jpg","Heart10.jpg","Heart11.jpg","Heart12.jpg","Spade0.jpg","Spade1.jpg","Spade2.jpg","Spade3.jpg","Spade4.jpg","Spade5.jpg","Spade6.jpg","Spade7.jpg","Spade8.jpg","Spade9.jpg","Spade10.jpg","Spade11.jpg","Spade12.jpg"];

var pickCard1 = cardArray[Math.floor(Math.random() * cardArray.length)];
var pickCard2 = cardArray[Math.floor(Math.random() * cardArray.length)];
if(parseInt(pickCard1.match(/\d+/g)) === parseInt(pickCard2.match(/\d+/g))){
    /*Do something here if the first equals the second card*/
} else if(parseInt(pickCard1.match(/\d+/g)) > parseInt(pickCard2.match(/\d+/g))){
    /*Do something here if the first card is greater than the second card*/
}else{
    /*Do something here if the first card is less than the second card*/
}