Skip to content

Instantly share code, notes, and snippets.

@alonextou
Last active November 17, 2018 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alonextou/39a30ba45ac0033692e3f29fd433dec6 to your computer and use it in GitHub Desktop.
Save alonextou/39a30ba45ac0033692e3f29fd433dec6 to your computer and use it in GitHub Desktop.
how to f(x)
# How to function f(x).
The most important programming concept is using functions with variables, such as f(x). By understanding how to f(x), you can interpret almost any code. Think of f(x) as a sentence, where f (function) is the verb, and x (variable) is the noun.
The function, or verb is the action, such as climb.
The variable, or noun is the person, place or thing, such as ladder.
Programming languages may look different, but share similar concepts.
In Javascript, functions (verbs) are typed followed by parenthesis: climb().
Nouns (variables) can be typed in those parenthesis: climb(ladder).
Before you use a function, you must define your variables, otherwise how would our game know what to climb, or how?
climb(thing) {
return 'You climbed the ' + thing)
}
In Javascript, variables are declared with the var prefix, followed by the variable name, defined by an equal sign.
var obstacle = "mountain"
Here are a few important variable types you should know:
Boolean
Number
String
Array
Object
Function
Boolean
The smallest variables type, used to determine if something is true or false, stored in the computer's native language, binary. In JavaScript, you can use the binary values 0 and 1, or the words true and false. For example, these booleans have the same value:
var climbing = 0
var climbing = false
Number
A set of digits, with optional decimals:
var height = 9000.1
String
A set of characters surrounded by double quotes. One example of a string is "Hello World", but don't print that, were building a game.
var obstacle = "mountain"
Gotcha: Be careful, the string "true" with quotes is different than the boolean true, without quotes.
Array
A collection of variables, surrounded by brackets and separated by commas. The variables in an array can be any of variable type, including other arrays. Here we declare an array of three strings:
var obstacles = ["ladder", "mountain", "stairway to heaven"]
Object
A set of properties and values, surrounded by braces. The property value can be the same type as these variable types, including other objects.
var obstacle = {
name: "mountain",
height:
}
var climb = function() {
var obstacle = "mountain"
return "I'm climbing a " + obstacle
}
climb()
var climb = function(obstacle) {
return "I'm climbing a " + obstacle
}
var obstacle = "mountain"
climb("mountain")
climb("ladder")
Concatenate is just a fancy word for combine two strings with the + variable, which is different than the + math operator.
print("1" + 2)
print(1 + 2)
In the first example, if you combine "1" + 2, you would print 12, because JavaScript tries to automatically combine different variable types. The second example prints three, because + is also a math operator.
This is the book I wish I had when I was learning to program. This book is for both beginners and experts, covering the everything from the fundamentals of programming to deploying a game for the web, Android, and iPhone.
My biggest pet peeve with most learning material is all the drivel between the steps I need to accomplish my goal. If an entire chapter dedicated to printing "Hello World" sounds like a waste of your time, this book is for you. My goal is to be as concise as possible, so that you enjoy reading and feel accomplished the entire way.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment