javascript - atlas institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf ·...

28
Javascript nuts and bolts

Upload: others

Post on 30-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

Javascriptnuts and bolts

Page 2: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

Programming

it’s hard

there is a steep learning curve

I have to learn a new language

Page 3: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

Words of encouragement

you can do it!

it is written in english! Lot’s of code is just shortened to save you time typing it charset =character set, href = hypertext reference, etc. programming is actually there to make your work easier…

Page 4: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what
Page 5: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what
Page 6: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

http://creative.colorado.edu/atlas2200/example/simpsons-loop.html

Page 7: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

“I want to roll around naked with javascript”

–local developer

Page 8: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

consolejs playground (similar to css)

experiment and try new things

get feedback on what js is doing (console.log)

bug fix

Page 9: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

js placementplace external js script after </body>

js executes AFTER html and css is rendered

or inside <head> and add defer attribute

Page 10: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

JavaScript rulesJS is case sensitivevar date = new Date(); Use camelCase to concatenate words getElementById

variables - start with lowercase letter var talkingDog ;

Objects - start with upper class lettervar date = new Date();

End statements with a semicolon;

//use comments (single line)

/* or multi-line comments */

Page 11: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

variablesstorage container for data

ALWAYS declare variables!

var = myName

myName = “Melvin”;

use shorthand (pretty much always)

var myName = “Melvin”;

Page 12: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

weakly typed language

don’t have to declare what type of content we are putting into a variable

variable = generic container that holds multiple data types

Page 13: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

Data Types

numeric - 1, 3.14, -5

string - “words and sentences” (straight single or double quotation marks)use escape (\) when you need to include quotations in your stringvar quote = “\“you are my sunshine\“”; boolean - true, false

Page 14: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

http://creative.colorado.edu/atlas2200/example/madlibs.html

Page 15: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

building a js mad libstart with a script!

user inputs text value into form

js grabs value and stores it as a variable

user clicks button and js grabs variables and generates story

Page 16: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

write out your mad lib

put into html comments for reference

Page 17: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

proof of conceptstart small with one mad lib entry(easier to test and bug fix)

get the code to work!

use inspect console to check for errors

add the rest of the mad lib entries (rinse and repeat)

Page 18: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

input and store info

html

web page

js

declare variable grab value of input textand store in variable

Page 19: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

create buttonhtml

web page

js

button listens for event

connect variable to html button

js

declare variable

event runs function

Page 20: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

functionsprocedure

block of code

set of statements that performs a task

executed when “something’ calls it

an event: button click, page load, etc.)

Page 21: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

define function

js

keyword name parameters

block of code runs every time button is clicked

Page 22: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

generate story

html

js

declare variable connect variable to empty divjs

write to variable write text add variable value

Page 23: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

add to function

click the button

boom

Page 24: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

no boom?open inspect and check console

is js connected? (test with console log)console.log("js in the house”);

not declared

wrong variable name (case-sensitive)

Page 25: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

source code (simple version)

http://creative.colorado.edu/atlas2200/example/madlib-simple.html

reference only - DO NOT COPY & PASTE

hand code - make mistakes

bug fix and learn from your mistakes

start with external js (see

Page 26: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

add rest of entries

Page 27: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

stylize the html formhttps://www.w3schools.com/css/css_form.asp

removes default

blue focus outline

text input

focus state

Page 28: Javascript - ATLAS Institutecreative.colorado.edu/atlas2200/pdf/week12-tech-js-madlib.pdf · console js playground (similar to css) experiment and try new things get feedback on what

focus state

buttonstyle

focus state