Good news and bad news:
The good news
Your code actually works.
If you replace
console.log(deal());
with
console.log(JSON.stringify(deal()));
then you'll get the result you expect.
The bad news
The Chrome console is "live" -- meaning it shows the current value of an object -- not the value at the time you logged it.
(If you ask me, I'd call this a bug -- but my vote doesn't count.)
You have two solutions.
Solution 1
Use console.log
and stringify
as I have done.
Solution 2
Make deal
return a new and different object by moving re-arranging your code slightly.
function deal() {
// MOVE THESE LINES INSIDE THE FUNCTION.
// empty array to store cards
var cards = [];
// var to start for loop
var i = 0;
// start for loop
for (i; i < 2; i++) {
// add a random card to the i^th index of cards
cards[i] = randomCard();
}
// value fo function is array of two cards
return cards;
}