How Do I Assign An Object To An Array Within Another Object?
I'm taking on the blackjack challenge and coming up with my own code. So here's what I've got so far ... my card generator ... var card = { suit: null, face: null, valu
Solution 1:
The problem is your expectation of what Object.create()
does.
It creates a shallow clone of the object. Arrays are assigned by reference, not value.
For example
var arrayOne = [];
var arrayTwo = arrayOne;
arrayOne.push('3');
alert(arrayTwo[0]); // This will give you 3. Both variables are pointing to the same array
So too:
var handTemplate = {
childArray: []
};
var handOne = Object.create(handTemplate);
var handTwo = Object.create(handTemplate);
// At this point, handTemplate, handOne and handTwo all point to the same array
handOne.childArray.push('3');
alert(handTwo.childArray([0])); // Again, you'll see 3, because there's only one array
One solution is to create hand
using a dedicated function:
function createHand() {
return {
storecard: [], // Now we get a brand new array every time createHand is called
count: 0,
total: 0,
hitCard: function () {
this.count += 1;
card.generateCard();
this.storecard[this.count] = Object.create(card);
this.total += this.storecard[this.count].value;
this.logHand();
},
logHand: function () {
console.log(this.storecard[this.count].face + ' of ' + this.storecard[this.count].suit);
console.log('Value = ' + this.storecard[this.count].value);
console.log('Count = ' + this.count);
console.log('Hand Total = ' + this.total);
}
};
}
var me = createHand();
var dealer = createHand();
Solution 2:
well this is a different approach but a good way to do it. i've written the code in a way you could learn something. as example.. you could fully get rid of the count variable since its equal to storecard.length. mind however that you still need to prevent dublicate cards. you can easily do it by generating all cards first, putting it into an array and then splicing cards out of it randomly untill the stack is empty.
var Card = function () {
this.suit = null;
this.face = null;
this.value = null;
this.generateCard();
};
Card.prototype = {
generateSuit: function (x) {
this.suit = ["Clubs", "Diamonds", "Hearts", "Spades"][x];
},
generateFace: function (y) {
this.value = Math.min(10, y);
switch (y) {
case 1:
this.face = "Ace";
break;
case 11:
this.face = "Jack";
break;
case 12:
this.face = "Queen";
break;
case 13:
this.face = "King";
break;
default:
this.face = y;
break;
}
},
generateCard: function () {
this.generateSuit(Math.floor(Math.random() *4));
this.generateFace(Math.floor(Math.random() *13 +1));
}
}
var Hand = function () {
this.storecard = [];
this.count = 0;
this.total = 0;
};
Hand.prototype = {
hitCard: function () {
this.count++;
var card = new Card();
this.storecard.push(card);
this.total += card.value;
this.logHand();
},
logHand: function () {
var card = this.storecard[this.storecard.length -1];
console.log(card.face + ' of ' + card.suit);
console.log('Value = ' + card.value);
console.log('Count = ' + this.count);
console.log('Hand Total = ' + this.total);
}
}
outcome will be:
> varme=newHand();
undefined
> vardealer=newHand();
undefined
> me.hitCard();
Ace of HeartsValue=1
Count = 1HandTotal=1
undefined
> me.hitCard();
6 of DiamondsValue=6
Count = 2HandTotal=7
undefined
> me.storecard
[ { suit: 'Hearts',
face: 'Ace',
value: 1 },
{ suit: 'Diamonds',
face: 6,
value: 6 } ]
Post a Comment for "How Do I Assign An Object To An Array Within Another Object?"