image

This is part 3 in a 3-part JavaScript Crash Course to help you prepare for the 21-Day Coding Challenge. The challenge is back on November 22, 2021, and it's your opportunity to learn basic JavaScript in just 15-30 minutes a day in a fun, free environment while racking up your points to win fabulous prizes. Are you up to the challenge? Register now.

Conditionals

Conditional statements allow us to make decisions with our code. They include the choices that must be made and then resulting outcome of those choices.

The most common conditional statement, and the one you’ll need to know for the challenge is the if else statement.

     if (condition) {
        // code to run if condition is true
     } else {
        //run some other code instead
     }

For example:

     let cookies = 1

     if (cookies < 2) {
        console.log("I'm still hungry. MOAR cookies!")
     } else {
        console.log("Feeling full, no more cookies for me!")
     }

     // "I'm still hungry. MOAR cookies!"

Practice

  1. Declare a variable called weather, and assign it to a string
  2. Write some code to check if the weather variable is equal to “raining”, and output a reminder to bring an umbrella
  3. Add an else if statement that checks if the weather is equal to “sunny”, and output a reminder to bring a hat
  4. Add an else statement that just says “I’m not sure what you should bring!”

Arrays

An array can hold multiple values within a single variable. This means that you can contain a list of values within an array and iterate through them.

There are a lot of things we can do with an array. But for now, there are two concepts we should focus on for now:

Creating an array:

     const cars = ["Saab", "Volvo", "BMW"];

Accessing an item in an array:

     const cars = ["Saab", "Volvo", "BMW"];

     console.log(cars[0]) // "Saab"
     console.log(cars[1]) // "Volvo"
     console.log(cars[2]) // "BMW"

Loops

Programming loops are all to do with doing the same thing over and over again — which we call iteration in programming speak. There are many types of loops but they more or less do the same thing, repeat an action a particular number of times.

    let iterable = [10, 20, 30];

    for (const value of iterable) {
      console.log(value);
    }

    // 10
    // 20
    // 30

    for (const value of iterable) {
        value +=1
        console.log (value)
    }

    // 11
    // 21
    // 31

Practice

  1. Create an array of your favourite movies (or books, or anything else you like)
  2. Loop over each movie and log its title to the console

What Next?

While there is a whole world of JavaScript for you to discover, for now, diving into the subjects above should prepare you to face the 21 Day Coding Challenge! And don’t worry, we’ll be providing hints along the way. If you’re looking for more guided prep try some of these courses:

Missed the rest of this series? Read part 1 and part 2.