This lesson, we're gonna be constructing a deck of cards, so first of all, let's go over to we've
we've got the variables and we're going to setup the suits for the cards.
So these are the various suits.
And this is all going to be dynamic.
So depending on what information we've got here, we can adjust and the deck of cards will adjust accordingly.
So we're just going to still start out with the typical deck of cards where it's got the four different
suits.
So that's spades, hearts, clubs and diamonds.
And then also we're going to create another three and this one can contain all the ranks.
So these are all the possible ranks that are card might have.
So we know that we've got aces.
There's two stories.
Four, five, six, seven, eight, nine, 10.
And this can also be customized.
So depending on what type of card ranks you want to throw into the mix, you can customize this and
it will customize the card output that you're going to be expecting.
So let's save that and go in to down here at the bottom or iron 80.
So after we've built all of the content and we can also put this into our function so that it's not
as messy.
So how about we do that where we're going to create another function that's going to be just called
build?
And all of this construction that we did in the last few lessons can be contained within there.
So just copy paste it in and then we're going to initiate the building of the game board.
So that's the first thing that we do when we initiate the function.
Just make sure that everything is still working properly.
And we also did remove out the HDD himself from the index file so that we only have that one I.D. with
game and it's constructing the gameboard.
So let's say that we need to do is we need to build the deck of cards, because this is going to serve
as the base for the content of the game.
So set up another function that's going to build the deck of cards.
And then we can create the function just below and then we can move these around as needed.
So not passing any parameters in there.
And this is just going to be a function that's going to take the content of those arrays that we created
and build a deck of cards out of it.
So let's set up an object that we're gonna call game deck using the game object that we've got that
we established earlier.
So this are all of our game content can go.
And I'm going to switch this over to the console because we're going to output the deck of cards into
the console and creating a few loops.
So we're letting I equals and you can also do a for loop.
But I did need to use the variables for the ranks.
So I'm going to keep it at this.
But you could also just do a for loop looping through all the items within the array for each.
So going as I is less than suit's length, and that's how many times we're going to initiate the loop.
Don't forget to increment I because we need to count.
And a way out of the loop.
And this is gonna be another loop inside where we're going to as we're looping through all of the suits.
We also want to loop through all of the ranks are all the possible ranks.
And this is what's making our deck dynamic.
So looping through that array and increment G by one.
And so it's going to give us each individual card A can see that where we've got the suits.
And this is under the eye and ranks.
And this is under J.
So this should produce 52 different results.
So you can see the script runs.
We've got the ace of spades, all of the spades, the hearts, the clubs, the diamonds.
Now let's create an object that we're gonna add to the deck as we're constructing.
And as we're looping through.
So create an object and we'll just call it card.
So we could out a number bunch of different values to the card itself.
And then within the card object, we've got the card suit and the suit is just going to be equal to
whatever the value of suits is.
We've got the card rank.
And this is all contained within the object.
And then we can use this during the gameplay.
So within the ranks and that's controlled by the variable G.
And then we've got card value.
So this one is a little bit tricky because we do have jacks, queens, kings.
But then we also have much new numbers in there.
So let's set up a variable and we're going to just call this temp value and let's work out what the
temp value is going to be.
So setting up that temp value variable with LET.
And first, let's check to see if it's not a number.
And by checking to see if it's not a number is a default function in JavaScript to check.
To see if it's a number or not.
And we've got an example here where it's checking to see if X and if it's not a number that's going
to return back.
Not a number.
And if it is a number, then we're going to multiply whatever the value of X is by 1000 and return it.
So that's an example of how that works.
And we can easily apply that within trinary operator.
So those of you that aren't familiar territory operators, they allow us to do a shortened type condition
where we've got our default JavaScript operator and three operands.
So that condition is followed by a question mark.
So it's kind of like doing if condition.
But we do it all within one statement and we've got that expression to execute.
So if it's true, it's going to return back.
This if it's false.
It's going to return back that.
And the example they give here is that we see if it's if get fee is true.
And that means that it's a member is true.
They're going to get paid two dollars, whereas false is ten dollars.
And if it's one, it's going to be two dollars.
Because one is true.
Zero is false.
So these can also be boolean values that it can take in.
So going back into our example.
So we're checking to see if is not a number.
And we're going to check whatever is being returned back within Rink's G.
So whatever that value is.
So if it hits being a jack, a queen or a king or an ace.
It's not going to be a number.
And in that case, we want to return back a value of 10.
But if it is a number, then we're just going to simply return back the value of the number.
And I'm going to move the console message down below the temp value.
I'll put that in the console.
So we'll see that they all get 10 assigned.
And as we know with in blackjack, Jack Queen King can be 10 as well.
And there is one other adjustment that we need to make is that ISAs can be either eleven or a one.
So we're gonna set a default value of eleven to that.
And this is where we're gonna use the temp value and equal that.
And do another trinary operator.
And this time the condition is gonna check to see if it's the value of ranks.
G is equal to a.
And if it is that we know that we need to update the temp value.
And if it isn't, then we're fine.
We could just keep it at the temp value.
So taking the value of ranks J and checking to see if it's equal in a.
And that means that we're just gonna make that adjustment.
So let's go down to where we have the A and we see that it's being returned back the value of an eleven.
And the reason we're setting at eleven.
And we know what Blakroc.
You can either an ace computer or a one or an eleven.
And we're gonna do that programmatically where we're going to adjust the value once we get over 21.
So until it's twenty over twenty one.
The value of a will be eleven.
And if we have multiple A's and we are going to be doing a function for that.
So that's coming up in upcoming lessons.
So once we've set up and we've established the values within the card object, we want to just simply
add it to the game deck.
And using JavaScript re push.
We're going to push it into the game deck array.
And once we're done.
We'll output the value of game deck.
Remove that console message.
Remove some of that spacing and save.
So that gives us a deck of cards.
And as we can see, we've got 52 cards.
And each one of these cards, they've got to suit rank and value.
So coming up next, we need to shuffle the cards because we don't want them coming out just as they
are.
So it's coming up in the next lesson where we can shuffle the cards after the deck is built.