Javascript 运行函数时获得意外输出。输出原型的代码
调用Javascript 运行函数时获得意外输出。输出原型的代码,javascript,function,prototype,Javascript,Function,Prototype,调用loadGame()函数时,它应该构建一副牌,发牌,然后显示牌。它能做所有这些,还有更多。当它运行向用户显示卡片的功能时,它输出代表卡片的数字以及整个页面上唯一原型的代码。为什么? Javascipt的一部分 //变量 var theDeck=新数组(); var discardPile=新数组(); var allHands=新数组(); //原型 Array.prototype.exists=函数(搜索){ for(i=0;i看起来问题在于使用for in语法运行本机JS数组。此语法用于
loadGame()
函数时,它应该构建一副牌,发牌,然后显示牌。它能做所有这些,还有更多。当它运行向用户显示卡片的功能时,它输出代表卡片的数字以及整个页面上唯一原型的代码。为什么?
Javascipt的一部分
//变量
var theDeck=新数组();
var discardPile=新数组();
var allHands=新数组();
//原型
Array.prototype.exists=函数(搜索){
for(i=0;i看起来问题在于使用for in语法运行本机JS数组。此语法用于对象,如果将其用于本机数组,它也将返回成员/方法,如函数“exists”
仅对(var i=0;iSide)使用注意:fetchCard
函数永远不会拾取最后一张卡,它应该从52张卡中拾取,而不是51张:var randomCard=Math.floor(Math.random()*52);
。谢谢。我已修复它。。。
//Variables
var theDeck = new Array();
var discardPile = new Array();
var allHands = new Array();
//Prototypes
Array.prototype.exists = function(search) {
for(i=0;i<this.length;i++)
if (this[i] == search) return true;
return false;
}
//Functions (Only the ones the that are needed for this question)
function buildDeck() {
var i = 0
for (x=0;x<=3;x++) {
for (y=0;y<=12;y++) {
//x is for the type (i.e. spades, hearts, ...)
//y is for the face value (i.e. nine, ten, jack, ...)
theDeck[i] = new Array(x,y);
i++ //Gets ready to add the next card in the deck.
}
}
}
function dealHands() {
var cardsOfHand = new Array()
for (x=0;x<=1;x++) {
for (y=0;y<=1;y++) {
allHands[x][y] = fetchCard();
discardCard(allHands[x][y]);
}
}
}
function discardCard(card) {
var totalCards = discardPile.length;
if (totalCard != 0) { totalCards++ }
discardPile[totalCards] = card;
}
function fetchCard() {
var usedCard = true;
while(usedCard == true) {
var randomCard = Math.floor(Math.random()*51);
usedCard = discardPile.exists(randomCard);
}
return randomCard;
}
function showHands() {
for (whoHand in allHands) {
var hand = allHands[whoHand];
var cards = "";
for (whichCard in hand) {
var card = hand[whichCard];
cards += "[" + card + "]"; //TEMP: Used for debugging.
}
id = "player" + whoHand + "cards";
document.getElementById(id).innerHTML = cards;
}
}
function loadGame() {
buildDeck();
dealHands();
showHands();
}
<fieldset>
<legend align="center">Dealer's Hand</legend>
<div id="player0cards"></div>
</fieldset>
<fieldset id="">
<legend align="center">Player's Hand</legend>
<div id="player1cards"></div>
</fieldset>