Loops, Functions and
Callbacks in JS
Loops
Calculate sum from 0 to 100
Loops
Dumb way
Loops
Better way - For loops
Loops
Better way - For loops
ans = 0
Loops
Better way - For loops
ans = 0
I=1
Loops
Better way - For loops
ans = 0
I=1
Loops
Better way - For loops
ans = 1
I=1
Loops
Better way - For loops
ans = 1
I=2
Loops
Better way - For loops
ans = 1
I=2
Loops
Better way - For loops
ans = 3
I=2
Loops
Better way - For loops
ans = 3
I=3
Loops
Better way - For loops
ans = 3
I=3
Loops
Better way - For loops
ans = 6
I=3
Loops
Better way - For loops
ans = 6
I=4
Loops
Better way - For loops
ans = 6
I=4
Loops
Better way - For loops
ans = 10
I=4
Loops
Better way - For loops
ans = very big value
I = 51
Loops
Better way - For loops
ans = very big value
I = 51
Loops
Better way - For loops
ans = very big value
I = 51
Loops
Great way to visualise this - http://latent ip.com/loupe/
fl
Functions
What is a function?
A function in JavaScript is a set of statements that performs a task or
calculates a value
It should take some input and return an output where there is some obvious
relationship between the input and the output.
Functions
Syntax?
Functions
Syntax?
Function keyword
Functions
Syntax?
Name of fn
Functions
Syntax?
Arguments
Functions
Syntax?
Function body
Functions
Syntax?
Return value
Functions
Another example
Functions
How to call a fn?
Function body
Functions
How to call a fn?
Calling function
Functions
Why do we need functions?
Functions
Why do we need functions?
Functions
You are repeating yourself (DRY)
Callback Functions
Step 1 - Can you call one function inside another function?
Callback Functions
Step 1 - Can you call one function inside another function?
Yes
Callback Functions
Step 1 - Can you call one function inside another function?
Yes
Callback Functions
Callback Functions
Is DRY being violated here?
Callback Functions
Solution
Anonymous functions