Beginner workshop
http://slides.com/rkotze/javascript-beginner
By: Richard Kotze
Designed to be easy to use
Very flexible language
Not JQuery
if(something == true){
//execute this
}else{
//or this
}
Assess if an expression is true
var joinTwoStrings = "string a" + " " + "string b";
Code academy course unit 2
https://www.codecademy.com/learn/javascript
//name of function
function myFunctionName(parameter)
{
//body of function
}
//name of function
var myFunctionName = function(parameter)
{
//body of function
}
Two ways to define a function
//name of function
function myFunctionName(parameter)
{
//body of function
}
//Call the function
myFunctionName("parameter value");
//name of function
function add2(num)
{
return num + 2;
}
//Call the function
console.log(add2(7));
for (var i = 0; i < 3; i++) {
sum = sum + i;
}
Work in pairs
Flow of workshop:
Create new "PhotoGallery.html" file
Open in Chrome browser
F12 for inspect tools
Use the console tab only
<script type="text/javascript">
// the magical code goes here
</script>
<div id="Photo">
</div>
<script type="text/javascript">
var Photo = document.getElementById('Photo');
Photo.innerHTML = "New photo gallery";
</script>
getElementById = used to find an element on the page.
Belongs to the document object
The DOM: Document Object Model
Used for accessing and updating the page
var image = new Image();
image.src = "/path/to/image.jpg"
Photo.appendChild(image);
JavaScript only ;)
var imageList = [
"http://lorempixel.com/400/400/cats/1",
"http://lorempixel.com/400/400/cats/2"]
Define a list of images 3 or more image URLs
Update the Photo.src to use the first image
<button id="PhotoLeft">Left</button>
Add two buttons for left and right
We can use HTML for this.
photoLeft.addEventListener("click", function(){
//click functionality goes here
});
Add button click event listeners
Allows the program to know a button has been clicked.
var photoPosition = 0;
photoLeft.addEventListener("click", function(){
photoPosition++;
});
Increment or decrement a variable tracking the photo position
Use this photo position variable to change the photo.src
We have made a working photo changer.
Lets manual test our photo gallery
Review the quality of our code
What happens if I click "right" button more times than there are photos in the array?
Reducing logic flow makes it easier to read code.
Both left and right photos event listeners have repeating logic. Lets be D.R.Y.
JavaScript 1 completed