0
我需要幫助!我正在製作一個按鈕,它可以發出五張隨機卡片,但是不能有任何重複的卡片。現在我用下面的代碼得到五張隨機卡片,但有時候會有重複的卡片。如何編輯代碼以使其不存在重複?謝謝!在交易手牌時沒有提及
enter code here
"use strict";
var rank = {
"1": "Ace",
"2": "Deuce",
"3": "Tray",
"4": "4",
"5": "5",
"6": "6",
"7": "7",
"8": "8",
"9": "9",
"10": "10",
"11": "Jack",
"12": "Queen",
"13": "King",
};
function getRank(i) {
return rank[i]
};
var suits = {
"1": "Hearts",
"2": "Spades",
"3": "Diamonds",
"4": "Clubs",
};
function getSuit(i) {
return suits[i]
};
var main = function() {
console.log(this.id);
if (this.id == "btn3") {
var random1 = Math.floor(Math.random() * 13) + 1;
var random2 = Math.floor(Math.random() * 4) + 1;
var random3 = Math.floor(Math.random() * 13) + 1;
var random4 = Math.floor(Math.random() * 4) + 1
var random5 = Math.floor(Math.random() * 13) + 1;
var random6 = Math.floor(Math.random() * 4) + 1;
var random7 = Math.floor(Math.random() * 13) + 1;
var random8 = Math.floor(Math.random() * 4) + 1
var random9 = Math.floor(Math.random() * 13) + 1
var random10 = Math.floor(Math.random() * 4) + 1
var output = getRank(random1) + " of " + getSuit(random2) + ", " + getRank(random3) + " of " + getSuit(random4) + ", " + getRank(random5) + " of " + getSuit(random6) + ", " + getRank(random7) + " of " + getSuit(random8) + ", " + getRank(random9) + " of " + getSuit(random10);
document.querySelector("div").innerHTML = output;
var buttons = document.querySelectorAll("button");
for (var i = 0; i < buttons.length; ++i) {
buttons[i].addEventListener("click", main);
}
});