如何消除代码中的未定义和其他问题
how to I get rid of undefined(s) and the other problems in my code?
我在一千次不同的尝试中尝试了一千种不同的方式,但我的JS代码不会按照我想要的方式出现。当我在Mozilla草稿栏中运行它时,我会得到"userHand未定义",第二个printHand也显示为未定义。有人能告诉我21点游戏中的错误在哪里吗?
function Card (s, n) {
var suit = s;
var number = n;
this.getNumber = function () {
return number;
};
this.getSuit = function () {
return suit;
};
this.getValue = function () {
if (number > 10) {
return 10;
} else if (number === 1) {
return 11;
} else {
return number;
}
};
}
var cardNames = {1:"Ace", 2:"2", 3:"3", 4:"4", 5:"5", 6:"6", 7:"7", 8:"8", 9:"9", 10:"10", 11:"Joker", 12:"Queen", 13:"King"};
var suitNames = {1:"Clubs", 2:"Diamonds", 3:"Hearts", 4:"Spades"};
var deal = function () {
var s = Math.floor(Math.random() * 4 + 1);
var n = Math.floor(Math.random() * 13 + 1);
return new Card(s, n);
};
function Hand(){
var cards = [];
cards.push(deal());
cards.push(deal());
this.getHand = function () {
return cards;
};
this.score = function () {
var score;
for (i = 0; i < cards.length; i++) {
score = score + cards[i].getValue();
}
for (i = 0; i < cards.length; i++) {
if (score > 21 && cards[i].getValue() === 11) {
score = score - 10;
}
} return score;
};
this.printHand = function () {
for (i = 0; i < cards.length; i++) {
var hand;
if (i === 0) {
hand = cardNames[cards[i].getNumber()] + " of " + suitNames[cards[i].getSuit()];
} else {
hand = hand + " and a " + cardNames[cards[i].getNumber()] + " of " + suitNames[cards[i].getSuit()];
}
} alert(hand);
};
this.hitMe = function () {
cards.push(deal());
};
}
var playAsDealer = function () {
var playDealer = new Hand();
while (playDealer.score() < 17) {
playDealer.hitMe();
}
this.printHand = function () {
return playDealer.printHand();
};
this.score = function () {
return playDealer.score();
};
};
var playAsUser = function () {
var playUser = new Hand();
this.printHand = function () {
return playUser.printHand();
};
this.score = function () {
return playUser.score();
};
var decision = confirm("Your hand is " + playUser.printHand() + ". Click OK to hit or Cancel to stand");
for (i = 0; decision !== false; i++) {
playUser.hitMe();
decision = confirm("Your hand is " + playUser.printHand() + ". Click OK to hit or Cancel to stand");
}
};
var declareWinner = function (userHand, dealerHand) {
if ((userHand.score < dealerHand.score) || userHand.score > 21) {
return "You lose.";
} else if (userHand.score > dealerHand.score) {
return "You win.";
} else {
return "You tied.";
}
};
var playGame = function () {
var user = playAsUser();
var dealer = playAsDealer();
declareWinner(user, dealer);
console.log("User got " + user.printHand());
console.log("Dealer got " + dealer.printHand());
};
playGame();
您在printHand()
上没有返回任何内容
我只是添加了返回语句并开始工作。看看这个小提琴
this.printHand = function () {
for (i = 0; i < cards.length; i++) {
var hand;
if (i === 0) {
hand = cardNames[cards[i].getNumber()] + " of " + suitNames[cards[i].getSuit()];
} else {
hand = hand + " and a " + cardNames[cards[i].getNumber()] + " of " + suitNames[cards[i].getSuit()];
}
}
//alert(hand); //remove this alert
return hand; // <----- solution
};
相关文章:
- 如何消除代码中的未定义和其他问题
- Jquery未定义函数正在停止其他操作
- 为什么 Math.log10 在某些系统上工作,但在其他系统上返回未定义
- 未定义GM_openInTab(或任何其他GM_函数)
- 为什么我的数组在我的 savetime 函数中未定义,但在其他函数中正常运行
- JavaScript 加载其他脚本 - 调用函数 - 未定义
- jQuery $.browser 是未定义的.尝试了其他解决方案,但没有奏效
- 必须返回有效的 ReactComponent.您可能返回了未定义的数组或其他一些无效对象
- jQuery,如果在其他函数中使用,则为未定义函数
- 在我为其他值使用替换函数之后,对变量进行未定义.在jquery中
- Meteor findOne查询在一个模板助手中返回未定义的结果.在其他模板助手中,同样的查询也能很好地工作
- 回拨是未定义的和其他故事
- 是否可以像扩展其他对象一样扩展未定义对象
- Javascript函数返回未定义的其他函数
- 在其他模块中使用require时获得未定义属性
- 在内容脚本中使用 chrome.tabs 或其他 chrome API 时,“无法读取未定义的属性”
- 执行.click()方法时jQuery$(element)未定义-在所有其他时间返回element
- JS未定义的错误索引.html到其他JS
- 变量在某些目的上是未定义的,而是为其他目的定义的
- 得到一个未定义的索引错误,不知道为什么完全不知道为什么,似乎找不到任何其他解决方案