--SOLVED--
var deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10];
alert(deck.length);
function getDeck() {
alert(deck);
}
When I run this in a website the first alert works but the second leaves an error message saying deck is undefined, I have tried other ways of writing this as well but they do not work, how do I access this array inside a function?
The idea is that it is a blackjack game and when I run the function newGame, it checks if there is less than 21 "cards" remaining. If so it "shuffles" the deck by resetting it to the way it was at the start:
function newGame() {
if (deck.length <= 20) {
var deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
}
}
My full code: http://jsfiddle.net/cAWw2/ Thank you to everyone that left an answer or comment, removing var from the first line fixed it, I shall now learn about hoisting since many of the comments mention it. I have very very little knowledge of JavaScript at this point in time.