GDI Logo

Intro to JavaScript Preview

Slides by Liz Krane / @LearningNerd

TechJobsLA Sponsors

Oct 29th & 30th:
JavaScript Weekend Workshop

tinyurl.com/jsweekend

For complete beginners (only prereq: basic HTML)

8 hours of hands-on instruction with mini projects

Learn how to build dynamic web apps

Learn CS fundamentals that apply to every language

Welcome!

Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.


Some "rules"

  • Every question is important.
  • Help each other.
  • Have fun.
  • Learn and return.

Tell us about yourself!

  • What's your name?
  • What awesome things do you hope to make with JavaScript?
  • What's one question you have about JavaScript?

Bookmark These Slides:

gdila.github.io/gdi-js-preview

Follow along on your laptop or phone!

What is JavaScript?

A quirky but powerful and ubiquitous programming language -- it's built into every modern web browser!

Originally a client-side language for making websites more interactive, but now so much more!

Popularity of Programming Languages as of June 2016

Stats by RedMonk, based on data from GitHub and StackOverflow

g.co/super

chrome.com/racer

Read their article on how it was built

A Few of My Favorite JavaScript Libraries

PaperJS.org

NodeJS.org

d3JS.org

jlord.us/sheetsee.js

johnny-five.io

leafletJS.com

letteringJS.com

underlineJS.org

phaser.io

createJS.com/soundjs

revealJS.com

smore.com/clippy-js

Let's build something with JavaScript!

How to open your web browser's JavaScript console

  • Chrome:
    • Windows: Ctrl + Shift + J
    • Mac: Cmd + Opt + J
  • Firefox:
    • Windows: Ctrl + Shift + K
    • Mac: Cmd + Opt + K
  • Safari / Mac: Cmd + Opt + C

// Now let's use JavaScript to interact with the HTML!

// First try clicking on the button to the right    > > >
// It doesn't do anything yet, but we'll fix that!

						

// The HTML for the button is:
// <button id="testbutton">Click me!</button>

// So we can create a variable for the button like this:
var button = document.getElementById('testbutton');

						

// The HTML for the button is:
// <button id="testbutton">Click me!</button>

// So we can create a variable for the button like this:
var button = document.getElementById('testbutton');

// and change the text inside it like this:
button.textContent = 'testing one two three!';
						

// The HTML for the button is:
// <button id="testbutton">Click me!</button>

// So we can create a variable for the button like this:
var button = document.getElementById('testbutton');

// and change the text inside it like this:
button.textContent = 'testing one two three!';

// Let's do the same with the box below the button:
var displayBox = document.getElementById('testdisplay');
						

// The HTML for the button is:
// <button id="testbutton">Click me!</button>

// So we can create a variable for the button like this:
var button = document.getElementById('testbutton');

// and change the text inside it like this:
button.textContent = 'testing one two three!';

// Let's do the same with the box below the button:
var displayBox = document.getElementById('testdisplay');

// and change the text inside the box too:
displayBox.textContent = 'JavaScript is magical!';
						

// Now let's define a new function that does this:
function sayHello() {
	displayBox.textContent = 'Hello, world!';
};

// And let's call the function to check that it works:
sayHello();
						

// Now let's define a new function that does this:
function sayHello() {
	displayBox.textContent = 'Hello, world!';
};

// Finally, let's run this function when the button is clicked!
button.addEventListener('click', sayHello);

// It has 3 parts:
//  1) the HTML element where the event happens (button)
//  2) the type of event ('click')
//    REFERENCE:
//    https://developer.mozilla.org/en-US/docs/Web/Events
//  3) the function to run (sayHello)
						

// The complete code:

var button = document.getElementById('testbutton');
var displayBox = document.getElementById('testdisplay');

function sayHello() {
	displayBox.textContent = 'Hello, world!';
};

button.addEventListener('click', sayHello);

// Tinker with it here:
// https://codepen.io/LearnToCodeLA/pen/ZONyjP
						

Display messages here


// Press W, A, S, and D on your keyboard to move the box!

var box = document.getElementById('movebox');

window.addEventListener('keydown', moveBox);

function moveBox(event) {
  if (event.keyCode == '83') {
	var currentTopPosition = parseInt(box.style.top, 10);
	var newTopPosition = currentTopPosition + 10;
	box.style.top = newTopPosition + 'px';
  }

  if (event.keyCode == '87') {
	var currentTopPosition = parseInt(box.style.top, 10);
	var newTopPosition = currentTopPosition - 10;
	box.style.top = newTopPosition + 'px';
  }

  if (event.keyCode == '68') {
	var currentLeftPosition = parseInt(box.style.left, 10);
	var newLeftPosition = currentLeftPosition + 10;
	box.style.left = newLeftPosition + 'px';
  }

  if (event.keyCode == '65') {
	var currentLeftPosition = parseInt(box.style.left, 10);
	var newLeftPosition = currentLeftPosition - 10;
	box.style.left = newLeftPosition + 'px';
  }

}
						

Group Googling Challenge:

Make a Box Turn Random Colors!


var colorBox = document.getElementById('colorbox');

colorBox.addEventListener('click', randomColor);

function randomColor() {

  // Your code here! Go Googling!

  // HINT 1: Find out how to generate a random hex color

  // HINT 2: Find out how to use JavaScript to change
  //         the background color of an element

  // HINT 3: Don't forget hex colors need a # in front!

}
							


// Did you learn anything interesting? :)

// HERE'S THE SOLUTION:

var colorBoxSolved = document.getElementById('colorboxsolved');

colorBoxSolved.addEventListener('click', randomColor);

function randomColor() {

 colorBoxSolved.style.background = '#' +
          Math.floor(Math.random()*16777215).toString(16);

}
// SOURCE:
// https://css-tricks.com/snippets/javascript/random-hex-color/
							

Thank you,
and happy coding!

tinyurl.com/jsweekend

Hope to see you Oct 29th and 30th!