On Github ongerit / tm-lunchandlearn
Slides at: http://bit.ly/tmlearn
There are many different Programming Languages.
Today we will be learning Javascript
But first, let's start from the basics...
Don't worry, it's not hard math! All you need to know are functions and variables.
eg. 2+1
or x+1
or.... f(x)=x+1
Let: f(x) = x + 1
What is f(1)?
f(2)?
Think of functions as input and output.
In computer programming, functions and variables can be named anything*
* With some restrictions, depending on the programming languageLet: thomasTheTicket(ticket) = tickets * 2
Our function is called thomasTheTicketthomasTheTicket(100) = ?
thomasTheTicket(100) = 200
Functions can also have more than one (or 0) argument(s).
Let: bobby(x, y, z) = x + y + z
bobby(1, 2, 3) = ?
bobby(1, 2, 3) = 6
Let: x = 1
Let: bob = 2
2 * x + bob = ?
= 4
Both x and bob are variable names. Variable names can be (almost) anything.
Let: myfunction123(a, b) = a + b
Let: hello = 100
myfunction123(hello, 1) = ?
myfunction123(hello, 1) = ?
myfunction123(100, 1) = ?
= 101
Let: abc = 1
Let: def = 2
If we say z = abc + def
z = ?
z = abc + def
z = 1 + 2
z = 3. Easy!
Let: x = 5
Now, let's say:
x = x + 1
What does this mean?
The = sign means assignment.
We take the value of the right side of the equals sign and assign that value to the variable on the left.
Let: x = 5
x = x + 1
Now substitute the variables on the right side (like before)
x = 5 + 1
x = 6
We changed the value of x!
So now x = 6
What if we do it again?
x = x + 1
x = x + 1
x = 6 + 1
x = 7
So, x = x + 1 means that we are increasing the value of the variable x by one. The right side is the old value and the left side is the new value.
Can you think of why we would need to change the value of a variable?
In computer programming, we often want events to happen only when certain conditions are met. This is what we call logic and conditionals.
These are often in the form of:
if some condition, then do something
Conditionals are where we use the boolean variable type. Booleans can be either true or false.
age = 10
x = age > 100
x = ?
is age > 100 true or false?
x = false
In Javascript, we create functions like this:
function f(x){ return x + 1; }
This is the equivalent of saying: f(x) = x + 1
Notice the return. This will give us the output value.
function f(x){ // We will return the value 1 greater than the given argument return x + 1; }The same function with a comment
If you want the computer to ignore a line you typed, prepend it with a //. This is called a single line comment.
Curly braces ({}) show the beginning/end of a function, and semicolons (;) show the end of a statement.
In Javascript, the if statement looks like this:
if (condition){ // Do something here }
Simple, right?
x = 10; y = "no"; if (x > 5){ y = "yes"; }
x > 5 is a boolean expression. That means that it can be either true or false. What is it in this case?
y = ?
y = "yes"
y is of string type