gpt4 book ai didi

javascript - names.forEach 不是函数

转载 作者:行者123 更新时间:2023-12-03 03:12:59 24 4
gpt4 key购买 nike

我正在尝试创建一副纸牌,但在运行 deckOfCards 时出现标题错误。

function deckOfCards() {
const names = ['A','2','3','4','5','6','7','8','9','10','J','Q','K'];
const suits = ['Hearts','Diamonds','Spades','Clubs'];
var deck = new Array();

function card(name, suits) {
this.name = name;
this.suits = suits;
}

function createDeck() {
suits.forEach(function (names) {
names.forEach(function (type) {
deck.push(new card(type, weight));
});
});
}

createDeck();
}

最佳答案

Ok, I think this is what you were going for. I changed deckOfCards to be an object. Then fixed the createDeck variable issues.

Javascript:jsFiddler

var deckOfCards = {
names : ['A','2','3','4','5','6','7','8','9','10','J','Q','K'],
suits : ['Hearts','Diamonds','Spades','Clubs'],
deck : []
}

function card(name, suits){
this.name = name;
this.suits = suits;
}

function createDeck(){
deckOfCards.suits.forEach(function (suit) {
deckOfCards.names.forEach(function (name) {
deckOfCards.deck.push(new card(name, suit));
});
});
}

createDeck();
console.log(deckOfCards.deck);

关于javascript - names.forEach 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46887037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com