
Its the start of a new week and a new challenge on
Codecademy... and quite literally a new challenge. I logged on to Codecademy this morning to find a new Courses page layout with lessons and projects grouped into sections and something called the Blackjack Challenge. Today's Codecademy Walkthrough steps us through the first part of the Blackjack challenge: "Deal 'em Up"
Apart from a few layout changes and improvements, the first thing to notice with the exercise screen is that the script and console section have had a bit of a revamp. There is now some client side validation of code which is helpful to prevent mistakes and to give you better feedback if something isn't quite right with your code. This week I don't think I made a single syntax error due to this improved feedback mechanism. Nice work guys! OK, on with the lesson.
Blackjack challenge: Deal 'em Up >> Section 1 Before we get started....
Exercise 1. Introduction
Instead of the usual "hit run when you're ready" we have a to do a bit of thinking with this introductory exercise. Now we have to actually return the text "I'm ready to build a blackjack game" in the console.
console.log("I'm ready to build a blackjack game!");
Exercise 2. Dealing
Now we have to refer back to the previous Dice Game project to lift the code used to generate random numbers. There's a tweak needed though as instead of returning a random number from 1 to 6 we need to return a number from 1 to 10.
var randomRating = Math.floor(Math.random()*10 + 1);
Exercise 3. More Dealing
Next we need to build a function that takes two random scores from 1 to 10 and sums them together. There are no parameters in the function, but when returning the function don't forget to include the empty brackets.
var twoRandomScores = function () {
var score1 = Math.floor(Math.random()*10 + 1);
var score2 = Math.floor(Math.random()*10 + 1);
return score1 + score2;
};
twoRandomScores();
Exercise 4. Give me my cards!
This exercise introduces concatenating a numeric value with a string to provide a sensible message by reintroducing the "string + variable concept.
var randomScore = Math.floor(Math.random()*52 + 1);
console.log("I got the score "+randomScore);
Exercise 5. Scoring System
The modulo operator is reintroduced to provide the mechanism to convert a random number from 1 to 52 into a face value of a card by dividing by 13 and returning the remainder.
var deal = Math.floor(Math.random()*52 + 1);
var final = deal % 13;
console.log(final);
Exercise 6. Scoring System Continued
The modulo operator is continued in this exercise to return whether a random number is odd or even. Remember to use the triple equal signs here as we are comparing the modulo result with zero. The exercise hint talks about using the OR operator, but I didn't use it or need it for this exercise. Here's my code.
var outcome;
var randomScore = Math.floor(Math.random()*40 + 1);
if (randomScore % 2 === 0){
outcome = "even";
} else {
outcome = "odd";
}
console.log(outcome);
Exercise 7. We think you're ready
Following on from the first exercise there is a brief review and what would have previously required us to hit the run button now asks us to confirm we are ready in code using the confirm command.
confirm("Are you ready?");
And that's the first part of the Blackjack challenge done. The lessons, projects, and challenges are coming thick and fast now, and if like me you are noticing an improvement in your coding ability and combined with the enhancements to the Codecademy website you will be rattling through the exercises at a good pace. I will attempt to get the next part of the challenge posted as a walkthrough later this week. In the meantime why don't you take a look at my previous walkthroughs and see if you can make a more elegant version of my code.
Have fun!
Previous Walkthroughs:
Codecademy Walkthrough - Starting a Startup
Codecademy Walkthrough - Hello New York
Codecademy Walkthrough - FizzBuzz Answers
Author:
Paul Saunders