简体   繁体   English

循环后函数在对象中返回未定义的值

[英]Function returning undefined values in an object after looping

I'm currently working on my first JS project, a BlackJack game. 我目前正在做我的第一个JS项目,一个BlackJack游戏。 I've written a function to create a deck, deck is an array of objects containing card's Suit, Value and Weight. 我已经编写了一个用于创建牌组的函数,牌组是包含卡片的西服,价值和重量的一系列对象。 As for the Weight, after looping the function returns undefined, Suit and Value are returned fine. 至于权重,循环后该函数返回未定义的值,则返回Suit和Value。 Here is my code: 这是我的代码:

// Creation of the deck
function deckCreate() {
  for (let i = 0; i < cardSuits.length; i++) {
    for (let j = 0; j < cardValues.length; j++) {
      let cardWeight = parseInt(cardValues[j]);
      if (cardValues[j] === "J" || cardValues[j] === "Q" || cardValues[j] === "K") {
        cardWeight = 10;
      } if (cardValues[j] === "A") {
        cardWeight = 11;
      }
      let card = {
        Suit: cardSuits[i],
        Value: cardValues[j],
        Weight: cardWeight[j]
      }; deck.push(card);
    }
  } return deck;
}

Beforehand I have the following variables created: 事先我创建了以下变量:

const cardSuits = ["&clubs;", "&hearts;", "&diams;", "&spades;"];
const cardValues = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"];
let deck = [];

The objects that are returned are: eg 返回的对象是:例如

0: {Suit: "&spades;", Value: "2", Weight: undefined}
1: {Suit: "&hearts;", Value: "10", Weight: undefined}
2: {Suit: "&clubs;", Value: "5", Weight: undefined}

And I can't seem to understand why the Weight is undefined. 而且我似乎无法理解为什么权重未定义。 Would appreciate any help Thanks! 希望得到任何帮助,谢谢!

Corrected, removing [j] 已更正,删除[j]

 // Creation of the deck function deckCreate() { for (let i = 0; i < cardSuits.length; i++) { for (let j = 0; j < cardValues.length; j++) { let cardWeight = parseInt(cardValues[j]); if (cardValues[j] === "J" || cardValues[j] === "Q" || cardValues[j] === "K") { cardWeight = 10; } if (cardValues[j] === "A") { cardWeight = 11; } let card = { Suit: cardSuits[i], Value: cardValues[j], Weight: cardWeight }; deck.push(card); } } return deck; } const cardSuits = ["&clubs;", "&hearts;", "&diams;", "&spades;"]; const cardValues = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]; var deck = []; console.log(deckCreate()); 

The cardWeight is a number and not an array, which you get from this line: cardWeight是一个数字,而不是一个数组,您可以从以下行获得:

// You are pulling a single value from an array here:
let cardWeight = parseInt(cardValues[j]);

so when you try to pass an index to it, you get undefined returned. 因此,当您尝试向其传递索引时,会返回undefined返回值。 Just access cardWeight as a single value: 只需将cardWeight作为单个值访问:

 // Creation of the deck let cardSuits = ["&Clubs", "&Spades", "&Diamonds", "&Hearts"]; let cardValues = [2,3,4,5,6,7,8,9,10,"J","Q","K","A"]; let deck = []; function deckCreate() { for (let i = 0; i < cardSuits.length; i++) { for (let j = 0; j < cardValues.length; j++) { let cardWeight = parseInt(cardValues[j]); if (cardValues[j] === "J" || cardValues[j] === "Q" || cardValues[j] === "K") { cardWeight = 10; } if (cardValues[j] === "A") { cardWeight = 11; } let card = { Suit: cardSuits[i], Value: cardValues[j], Weight: cardWeight // <-- This is a single value, not an array }; deck.push(card); } } return deck; } console.log(deckCreate()); 

将权Weight: cardWeight[j]更改为权Weight: cardWeight

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM