Jeff Lee
@RebootJeff
This class will:
This class will NOT:
Survey:
(go to exercise site)
(go to exercise site)
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
(live demo)
<body style="background-color:lightgrey">
<h1 style="color:blue">This is a heading</h1>
<h1 style="color:#AA22FF">Also a heading</h1>
<h1 style="color:rgb(0,255,255)">Moar!!</h1>
<p style="color:red;background-color:green">This is a paragraph.</p>
<img src="https://i.imgur.com/81qyN1y.jpg" style="height:100px;width:100px">
</body>
"My (fictional) vacation"
Best Practices:
<nav>
<article>
In HTML5, we have more
meaningful variants of `<div>`
<section>
<aside>
<header>
<footer>
HTTP Request Methods
Meanings
...specified resource (URL)
HTTP Request Methods
Database
Actions
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew update
brew doctor
brew install node
node -v
npm -v
false // boolean
0 // number zero
"" // empty string
undefined
null
NaN
!!(someValueOrVariable); // convert to Boolean
!!0; // false
!!""; // false
var x = 0;
!!x; // false
x = ""; // false
!!x; // false
if(cond1) {
if(cond2) {
// do stuff
}
}
if(cond1 && cond2) {
// do stuff
}
!(num > 35);
num <= 35;
(!cond1 && !cond2);
!(cond1 || cond2);
(!cond1 || !cond2);
!(cond1 && cond2);
What foods do you like?
var isMean = prompt('Is he/she a meanie?');
var isDumb = prompt('Is he/she a dummy?');
var isDateable = !isMean && !isDumb;
// or you can write...
var isDateable = !(isMean || isDumb);
var message = 'He/she is' +
(isDateable) ? ' ' : ' not ' +
'dateable';
alert(message);
// or you can write...
var message = 'He/she is ';
if(isDateable) {
message += 'not ';
}
message += 'dateable';
// Counting from 1 to 5...
console.log('1');
console.log('2');
console.log('3');
console.log('4');
console.log('5');
There has to be a better way!
(╯°□°)╯︵ ┻━┻
// Counting from 1 to 5...
var num = 1;
while(num < 5) {
console.log(num);
num = num + 1;
}
By the way, we can increment `num` in multiple ways
var password = prompt('Enter password');
while(password !== 'I<3JavaScript') {
password = prompt('Enter password');
}
var paragraph = document.querySelector('#greeting');
paragraph.textContent = 'Correct password!';
There has to be a better way!
(゜_゜)
var password;
do {
password = prompt('Enter password');
} while (password !== 'I<3JavaScript');
var paragraph = document.querySelector('#greeting');
paragraph.textContent = 'Correct password!';
Much better! (◠‿◠)
// 'noprotect'
var password;
do {
password = prompt('Enter password');
} while (password !== 'I<3JavaScript');
console.log('correct!');
Might need "// 'noprotect'"
...with Pair Programming
...and feel free to ask about any new syntax
// Counting from 1 to 5...
for(var i = 0; i < 5; i++) {
console.log(i);
}
// Counting from 1 to 5...
var num = 1;
while(num < 5) {
console.log(num);
num = num + 1;
}
Although, `$` is actually much more powerful than `document.querySelector`
Lost Macbook charger?
Curriculum website URL changed
Different TA
(Mental) Thumbs?
var funcName = function(parameter1, parameter2) {
var someReturnValue = parameter1 + parameter2;
return someReturnValue;
};
// "call" or "invocation"
funcName(argument1, argument2);
var add = function(num1, num2) {
var sum = num1 + num2;
return sum;
};
var add = function(num1, num2) {
return num1 + num2;;
};
var result = add(1, 2);
So many events! (check MDN)
but we'll stick to DOM-user interaction events.
// Guess what? There is a <button> HTML tag!
var button = document.querySelector('button');
button.addEventListener('click', function() {
// run some code here
});
Imagine subscribing to an email newsletter
Thumbs?
var button = document.querySelector('button');
Thumbs?
var runSomeCode = function() {
// run some code here
});
button.addEventListener('click', runSomeCode);
Option 1:
Option 2:
button.addEventListener('click', function() {
// run some code here
});
var selector = '#some-button';
var identifyTarget = function(event) {
console.log('What you clicked: ' + event.target);
};
// best practice
$(selector).on('click', identifyTarget);
// not as good
$(selector).click(identifyTarget);
With jQuery, stick to `.on()`
Strings are collections of characters
var myString = 'abc';
// Tricky: zero-indexing
myString[0] === 'a';
myString[1] === 'b';
myString[2] === 'c';
myString[3] === undefined;
var myString = 'abc';
myString.length === 3;
// What if we do...
myString.length = 2;
myString; // ???
var index = 1;
myString[index]; // ???
myString[99]; // ???
How can we get the last character of a string?
Let's use the
length property!
Arrays are also collections with numeric indices
var myArray = ['hello', 37, true];
myArray[0] === 'hello';
myArray[1] === 37;
myArray[2] === true;
myArray[3] === undefined;
These are called
array elements.
We're accessing them via bracket notation.
var index = 1;
myArray[index]; // ???
myArray[99]; // ???
myArray.length === 3;
// What if we do...
myArray.length = 2;
myArray; // ???
var myString = 'abc';
myString.split('b') === ['a', 'c'];
myString.toUpperCase() === 'ABC';
// What about the opposite?
var myArray = ['Z', 83, true];
myArray.join('-') === 'Z83true';
myArray.push('hello'); // ???
// What about the opposite?
...and many, many, many more!
What happens to the original string?
What happens to the original array?
aka "iterating over elements of an array"
var myArray = ['hello', 37, true];
var index = 0;
var element;
while(index < myArray.length) {
element = myArray[index];
console.log(element);
index++;
}
Option 1:
While Loop
Option 2:
For Loop
Option 3:
forEach Loop
(not for strings)
for(var i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}
myArray.forEach(function(element, index) {
console.log(element);
console.log(index);
});
var original = 'Yes';
var copy = original;
copy = 'No';
original; // ???
Strings are not mutable.
Numbers and booleans are not mutable either.
var original = ['a', 1, false];
var copy = original;
copy.push('hi!');
original; // ???
Arrays are mutable!
Objects are collections with string keys
var myArray = ['hi', 37, true];
myArray[0] === 'hi';
myArray[1] === 37;
myArray[2] === true;
myArray[3] === undefined;
Arrays are collections with numeric indices
var myObj = {
'firstName': 'Jeff',
'twitterHandle': '@RebootJeff'
};
myObj['firstName'] === 'Jeff';
myObj['twitterHandle'] === '@RebootJeff';
myObj['lastName'] === undefined;
// empty object
var myObj = {};
var myObj = {
'propertyName': 'propertyValue',
'key': 'value'
};
Property
aka
Key-Value Pair
var myObj = {
'firstName': 'Jeff',
'twitterHandle': '@RebootJeff'
};
myObj['firstName'] === 'Jeff';
myObj['twitterHandle'] === '@RebootJeff';
myObj['lastName'] === undefined;
myObj.firstName === 'Jeff';
myObj.twitterHandle === '@RebootJeff';
myObj.lastName === undefined;
var myObj = {
firstName: 'Jeff',
twitterHandle: '@RebootJeff'
};
Caveat: Quotes still needed for keys with special chars
var myObj = {
'firstName': 'Jeff',
'twitterHandle': '@RebootJeff'
};
var key = 'firstName';
myObj[key] === 'Jeff';
var myObj = {
firstName: 'Jeff',
twitterHandle: '@RebootJeff'
};
var value;
for(var key in myObj) {
value = myObj[key];
console.log(value);
}
var myArray = {
'0': 'hi',
'1': 37,
'2': true
};
myArray[0] === 'hi';
myArray[1] === 37;
myArray[2] === true;
myArray[3] === undefined;
Arrays are just a special kind of Object where the keys are all numeric instead of alphanumeric
Bracket notation automatically converts the index into a string
Come with questions
or
Work on unfinished exercises
or
Work on new hackathon exercises
But forget about the eXtensible Markup Language part.
Examples of JSON here (but not 100% real data)
Questions?
console.log('outside a function:', this);
var logThis = function() {
console.log('inside a function:', this);
};
logThis();
var myObj = {
somePropertyKey: 'some property value',
someMethodName: logThis
};
myObj.someMethodName();
Usually*, it's easy to see the context by looking at what's left of the dot at call time.
var logThis = function() {
console.log(this);
};
var myObj = {
somePropertyKey: 'some property value',
someMethodName: logThis
};
myObj.someMethodName();