Skip to content

Instantly share code, notes, and snippets.

@cpave3
Created January 31, 2020 02:03
Show Gist options
  • Save cpave3/4042e77a978e5828eceb48e6e67b2128 to your computer and use it in GitHub Desktop.
Save cpave3/4042e77a978e5828eceb48e6e67b2128 to your computer and use it in GitHub Desktop.

Lesson 1

Values

Values are the raw data of your application. A value is a representation of an enitity, which the program can manipulate. They are generally atomic units of data, but in some cases can be more complex, and even composed of other values.

There are multiple different sorts of values, and they are differentiated by what is known as a type. Examples of common types are:

  • String - A string is essentially a bit of text. they are usually represented by some text wrapped in either "double quotes" or 'single quotes'. There are other kinds of quote marks which can denote strings as well, but these two are the most common

  • Integer - an Integer is a whole number, essentially. 5, 10, 100000 are examples of integers

  • Float - a Float is another kind of number, so named for the floating point which distinguishes it from an Integer. Floats have fractions following a decimal point. examples include 10.405, 3.14, 0.0001, etc

  • Arrays - an array is basically a list of other values. Arrays are denoted by the square brackets which surround them. the items in an array can be of any type, like so ['some text', 1, 2, 3.14, 'some more text']. The comma separates each of the items, so in this case, the array contains 5 items.

  • Objects - an Object is a collection of properties, where a property is an association between a name (or key) and a corresponding value. for example, consider the following: { myKey: 'my Value' }. The values can be of any type, such as the ones listed above here. There are some limitations around what you can use as a key, but generally speaking if you stick with a short, descriptive (of the purpose of the value) word, You will generally do okay.

  • Boolean - a boolean is a value which is either true or false. They are binary, and are often used for conditial statements

Variables

To create a variable, we use the var keyword, like so:

var greeting;

This initialises the variable and tells the computer to set aside some memory for us that we can refer back to with this name. think of it like an address. Currently there is nothing in this variable though, but we can put a value in it with the = operator. This is called assigning.

greeting = "Hello there!";

We can also assign the value when we initialise like so:

var myFruit = 'apple';

Variables can be used anywhere that a value can, so we can use it like this:

var greeting = 'Hello there';
console.log(greeting);

Because we can use it ANYWHERE we can use a value, we can also do this:

var myFruit = 'apple';
var myLunch = myFruit;

Operators

Operators are a type of construct which represents an action or relationship. They are conceptually very similar to mathematic operators, in fact some of them are the same

  • + addition
  • - subtraction
  • * multiplication
  • / division
var sumOfNumbers = 1 + 3;
alert(sumOfNumbers);

This will pop up the number 4

functions

A function is a named section of code that performs a specific task as described by its name. They can take inputs (called arguments) and return a value. Sometimes they won't return a value though and that's okay. If a function doesn't return a value, we say that it "returns void" or "is void".

the value returned by a function can be assigned to a variable, and used by other bits of the code.

console.log is a function. It writes some data to the console, but doesn't return a value for us to use, so it returns void

You can make your own functions too, like this:

function doubleNumber(number) {
    var doubled = number * 2;
    return doubled;
}

var myNumber = doubleNumber(4);

alert(myNumber);

What will the pop up say? can you guess?

conditional statements

Conditional statements allow you to run a bit of code if a certain condition is satisfied. Conditional statements are an important part of structured programming and let us make much more dynamic software.

There are a number of operators that help use make certain conditions, and these conditions will result in a boolean value for us to evaluate. The most common conditional operators are:

  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to
  • == Equal to
  • != Not equal to
  • === Identical to

NOTE: the assignment operator = is different to the conditional operator ==. event though they look similar, the single one is for assigning values, and the double one is for comparing values.

consider the following code:

var userAge = window.prompt('How old are you?);

if (userAge >= 18) {
    alert('You are an adult');
} else {
    alert('you are legally a child');
}

conditional statements make use of the if/else keywords to set up control flow.

If the condition inside the parenthesis next to the if keyword evaluate to true, then that block will run, otherwise the else block will run if present (you don't need to include an else block)

Arrays

As mentioned before, an array is like a list. they are good for storing collections of data.

if we have an array of names and we want to get (or replace a specific one) we can target a specific item in the array by using what is called bracket notation. consider the following:

var names = ['Scott', 'Christina', 'Joel'];
var joelName = names[2];
alert(joelName);

NOTE: remember, in programming the first number is always 0, so if you want the 3rd item, that is going to be number 2.

We can also add new items to an array by using the push method built in to the array, like so:

var animals = ['rabbits', 'cats', 'dogs'];

animals.push('chickens');

console.log(animals);

We say that an array is "iterable" because we can "iterate" over it with loops and functions.

loops

A loop is a construct which lets us execute the same code multiple times with slightly different parameters each time.

There are two main types of loops.

While Loop

These loops will run until a condition is no longer met. Be careful with this, as they can run forever!

var count = 0;
while(count < 10) {
    console.log(count);
    count = count + 1;
}

For Loops

These loops will run a set number of times, according to the counter declared in the statement

for (var i = 0; i < 10; i = i + 1) {
    console.log(i);
}

🤔 What is happening here?

The statement (the bit in between the brackets) has 3 parts. those parts are separated by the semi colons ;. The first part sets up the counter for the loop, the second part defines the condition for the loop to execute until, and the third part is responsible for iterating the counter at the end of each iteration.

We can use this to iterate through an array, too. for example:

var animals = ['Cats', 'Dogs', 'Geese'];

for(var i = 0; i < animals.length; i++) {
    var animal = animals[i];
    if (animal === 'Geese') {
        console.log('I do not love ' + animal);
    } else {
        console.log('I love ' + animal)
    }
}

Exercise: Fizz Buzz

Count from 1 to 50 and print the numbers out:

  • If a number is a multiple of three, print ‘Fizz’.
  • If it’s a multiple of 5, print ‘Buzz’.
  • If it’s a multiple of 3 and 5, print ‘FizzBuzz’.

For everything else, print the number itself. Note: You may wish to use arithmetic operator remainder (%):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment