JavaScript Syntax and Programming Concepts

Arithmetic Operators

*Modulus divides two numbers and returns the remainder.

Back to top


A single line comment is written like this:

//type your comment here

A multiple line comment is written like this:

/* part 1 of comment part two of comment part three of comment */

Comment everything so you don't forget what something does!

Back to top


An array is a numbered list of data. It has different positions and a value can be stored at each position. The first position is number 0.

To access a particular position in an array to set or retrieve its value, write


where position is a number or variable representing a number.

To set an entire array, write

arrayName = array(value1, value2, value3, ...)

where each value is the value to be set to that position, counting positions from 0. You can also use a for loop to go through the array and set each position individually.

To find the size of an array, write


Back to top

Boolean Expressions

Boolean comparisons:

*Equals is not just =. = is used for setting variables. === is used for comparisons.

Combining boolean comparisons:

Remember to use parentheses to separate components of longer expressions.

*The vertical line can be typed by pressing shift and \. \ is located above your return/enter key.

Back to top

For Loops

A for loop is a modification of a while loop that makes it easier to do something a certain number of times. The basic for loop structure is:

declare counter; for(initialize counter; counter boundary (boolean expression); counter incrementation){ //do something here }

var counter = 0; for(counter = 0; counter < 3; counter++){ //do something here }

This for loop will do something three times (once at counter === 0, once at 1, and once at 2). Make sure the boundary condition will be reached or you will get an infinite loop and the program will crash. Keep in mind that you have access to counter inside the for loop.

Back to top

Function Definitions

To define a function with no parameters, write

function functionName(){ //code goes here }

To define a function with one parameter, write

function functionName(parameter){ //code goes here }

To define a function with more than one parameter, write

function functionName(parameter1, parameter2, parameter3){ //code goes here }

Make sure you assign the parameters in the correct order when calling your function!

Back to top

If-Else Statements

The basic structure for an if-else statement is:

if(booleanExpression1){ //do first thing } else if(booleanExpression2){ //do second thing } else{ //do third thing }

If booleanExpression1 is true, the first thing will be done and the second and third things will be skipped. If it is false and booleanExpression2 is true, the second thing will be done and the first and third will be skipped. If neither boolean expression is true, the third thing will be done and the first and second will be skipped.

Back to top

Incrementation Shortcuts

variable = variable + value;

is the same as
variable += value;

This can be done with every arithmetic operator.

variable = variable + 1;

is also the same as

variable = variable - 1;

is also the same as

Back to top

Naming Rules and Conventions

Back to top

Variable Declaration

To declare a variable, write

var variableName = value;

You only write var when declaring a variable. If value is a string, it must be in quotes.

Back to top

While Loops

A while loop is used to repeat a section of code over and over. Its basic syntax looks like this:

while(booleanExpression){ //do something }

The code in the while loop will be executed as long as booleanExpression is true. If it never becomes false, the program will enter an infinite loop and crash. While loops can be used for doing things a certain number of times, but a better option in that case is often a for loop.

Back to top

Variables and Functions that We Wrote for You

We've written many different pieces of code to allow your program to interact with the game and the dealer. You have access to the following functions:

You have access to the following boolean variables:

You have access to the following string variables:

You have access to the following arrays:

Back to top

Rules of Blackjack

Every player tries to get a hand with a value of as close to, but no more than, 21 as possible. Face cards are worth 10 points, the Ace can be 1 or 11, and every other card is worth its face value. You play against a dealer. You and the dealer will each be dealt two cards at the beginning of the game. You will be able to see one of the dealer's. You will then have the option to "hit" (get another card) as many times as you'd like. Once you don't want any more cards, you "stand." Be careful! If the value of your cards exceeds 21, you "bust," that is, you lose! Once you stand, the dealer will draw cards. The dealer must hit if the total value of his/her cards is less than 17, and must stand if it is 17 or greater. When determining this, an Ace is counted as an 11. If the dealer busts and you don't, you win. If no one busts, whoever has a higher hand wins. If either player is initially dealt 21, that's a "Blackjack" and that player wins. If the players tie, the dealer wins (unless both players have Blackjack. Then it's just a tie.) If you get 5 cards without busting, you win. You bet at the very beginning of a hand, before you are dealt cards. If you lose the hand, the dealer takes your money. If you win, you get your money back and the dealer pays you as much as you bet. If you win with a Blackjack, the dealer pays you 150% of what you bet. You can also "double down." This means you double your bet and then only get one more card. You can only double down immediately after being dealt to; That is, you cannot hit and then double down. If you want more information, click here.

Back to top