alert(‘Hello World!’);
var name = prompt(‘what is your name’);
alert(‘Hello’ + name + ‘, it is great to meet you!’);
2*3;
var x = 3;
x = 4;
var y = 2;
x * y;
// this will result in 8
parseInt(prompt(‘what year were you born?’));
var age = prompt(‘how old are you?’);
if (age >= 21) {
alert(‘you can drink!’);
} else {
alert(‘you can not drink’);}
// this is a JavaScript comment
Math.random()
Math.floor()
Math.floor(Math.random()* 10) + 1
index.js
playground.html
alert(‘Hello World’);
Function – Check customers IDs
Count number of customers in line
Until you have checked all customers, check each id
Function – Check ID
If user is under 21, they can’t drink, otherwise they can
alert(‘hello’)
// send user message
prompt(‘enter info’)
// get input from user
console.log(‘test’)
// good for debugging; sends message to console
parseInt()
// turn string into number
// random # between 1 and 10
var color = ‘white’;
// assignment operator
var weight = 30*15;
// arithmetic operators; declares and sets weight to 450
greeting = ‘Hi’ + ‘John’;
// string operator; sets greeting equal to ‘Hi John’
buy = 3 > 5;
// comparison operator…buy=false
buy = (5>3) && (2>4);
// logical operators; and statement; buy = false
buy = (5>3) || (2>4);
// logical operators; or statement; buy = true
i++;
// adds one to current number
x = x +5;
// adds 5 to x; if x was 12, then x would be set to 17
i--;
// subtracts one from current number
8 % 5;
// returns the remainder of 3
2**4;
// exponent of 2^4, which equals 16
if (today - user_birthday > 21 ) {
print(‘you can drink’);
} else if (user_birthday < =0) {
print(‘not a valid ID’);
print(‘you can’t drink);
}
var sum = 0;
for (x=1 x <= 100; x++) {
sum = sum + x;
alert(sum);
// this will show the number 5050
while (still_playing == true) {
play_game();
switch (response) {
case: blue return ‘b’;
case: red return ‘r’;
case: yellow return ‘y’;
‘I am a string’
“I’m a string too”
‘I’m a syntax error!!’
str.length;
// returns the number of characters in a string
push(), pop(), splice(), length, and indexOf().
var new_list = [];
// declares a new list
new_list = [2, 81, 52];
// sets new_list equal to list containing three numbers: 2, 81, and 52
new_list[1];
// returns the number 81
new_list[1] = 82;
// changes the element in position 1 (i.e. 81) to the number 82
new_list.push(3);
// adds the number 3 to that list
new_list.pop();
// removes and returns the last item from that list
new_list.splice(1,2);
// starting at index position 1, removes two elements from list
new_list = [‘apple’, ‘banana’, ‘orange’]
// creates a new list called groceries
new_list.splice(1,0,’grapes’);
// adds grapes to left of index 1; i.e. [‘apple’,‘grapes’,‘banana’,‘orange’]
new_list.length;
// returns the number of elements in an array
new_list.indexOf(‘banana’)
// searches array for apple
var quick_dict = {‘tiger’: ‘a large cat’, ‘ice cream’: ‘a tasty desert’};
var my_hotel = {‘address’: ‘111 main street’, ‘rooms available’: 13};
my_hotel[‘address’];
// looks up the key ‘address’ and returns the value for that key, ‘111 main street’
function add_numbers(x,y) {
print(x+y);
add_numbers(1,2);
// outputs 3
function MovieStar(name, birthday) {
this.name = name;
this.birthday = birthday
this.greeting = function(){
alert(‘hi, my name is ‘ + this.name);
var tom_cruise = new MovieStar(‘Tom Cruise’, ‘July 3, 1962’);
var dwayne_johnson = new MovieStar(‘Dwayne Johnson’, ‘May 2, 1972’)
tom_cruise.birthday;
// returns July 3, 1962
tom_cruise[“birthday”];
tom_cruise.birthday = ‘July 4, 1962’;
tom_cruise.greeting();
// calls the greeting method; alerts user ‘hi, my name is Tom Cruise’
var star_list = [tom_cruise];
// adds tom_cruise object to star_list
star_list.push(dwayne_johnson);
// adds dwayne_johnson object to star_list
for (p in person) {
alert(p);
<tags>
<p>
</p>
<p> Hello World </p>
<p class=”greeting”> Hello World </p>
p {font-size: 24px;}
.greeting
{color:pink;}
color
pink
.greeting {color:pink;}
<!-- Comments are placed in between the following -->
<!doctype html>
<!-- needed once for all websites; tells browser to expect html code -->
<html>...</html>
<!-- surrounds all the code for your website’s html file -->
<head>...</head>
<!-- includes all the browser information (e.g., doc title, technical information) -->
<meta charset=”UTF-8”>
<!-- Goes inside head tag for U.S. websites -->
<title> …</title>
<!-- Shows up in the web browser title; Goes inside the <head> tag -->
<link href="main.css" rel="stylesheet">
<!-- Link to external style sheet; put this in head section -->
<body> …</body>
<!-- Includes all the content of the website -->
<header> … </header>
<!-- Often has navigation bar & site logo; this is different from <head> -->
<nav> …</nav>
<!-- Used to group together major navigational blocks on a page; Often put inside the header -->
<a href=’www.fb.com’> fb</a>
<!-- links to other sites or internal sites -->
<h1> … </h1>
<!-- Header 1 used typically in <header> tags -->
<main>… </main>
<!-- General for main part of the website -->
<h2> …</h2>
<!-- Header 2 used typically in <main> tags for sub-headers, can also use <h3>, <h4>, <h5> -->
<p> …</p>
<!-- chunks of texts on site; html is whitespace insensitive -->
<article> …</article>
<!-- Used for unique part of site (e.g., used for individual blog post) -->
<aside>... </aside>
<!-- Not required to understand the rest of the page -->
<section> … </section>
<!-- use for section tag -->
<div> </div>
<!-- Allows you style a group of elements with the same CSS. It’s also a great multi-purpose tag. It is a non-semantic tag unlike other tags like <ul> or <article>, which are semantic tags that tell you something about the element; It is also a block-line element by default; It is generic and can be used in a wide range of activities. It is used when none of the above apply. -->
<footer> ….</footer>
<!-- Web page footer -->
<ul> <li> point 1 </li> <li> point 2 </li> </ul>
<!-- Unordered lists: bullet points; put each List Item <li> within <ul> -->
<ol> <li> step 1 </li> <li> step 2 </li> </ol>
<!-- Ordered lists: bullet points with numbers instead of bullets -->
<img src=”images/gummy.jpg”>
<!-- image found in image folder; use “../images/crispy.jpg” if your index.html is not in same directory -->
<head>
<link href="normalize.css" rel="stylesheet"><
p {color:purple;}
font-family: “georgia”, “serif”;
/* If Georgia font does not load on person’s browser, serif is back up font */
color:blue;
color:#630600;
color: rgba(255,255,255,1);
/* Uses color on a 0 to 255 scale for R G B and then 0 to 1 for the last value for transparency */
font-weight: bold;
font-weight: 600;
font-style: italic;
font-size:16px;
text-transform: uppercase;
text-decoration: underline;
font:bold 14px Tahoma;
text-indent:25px;
text-align: center;
background-color: rgba(0,0,0,0.5);
background-image: url("http:dash.ga.co/assets/anna-bg.png");
background-image: url(“../images/header_bg.jpg”);
/* select an image that is up one folder in structure and in images */
background-size: cover;
background-repeat: no-repeat;
background-repeat: repeat-x;
/* Left Right Tile */
background-repeat: repeat-y;
/* UpDown tile */
background-position: 10px 60px;
/* Change starting position */
background-position: 50% 60px;
img {float:right;}
/* Element float images to right or left or none */
p {clear:none;}
/* Allows floating elements on both sides. This is default. */
p {clear: left;}
/* No floating elements allowed on the left side. Also can use right or both - No floating elements on either side */
p {clear: inherit;}
/* Element inherits the clear value of its parent */
max-width: 500;
/* Allows website to be responsive & have 500 max width */
width:60%;
height:5%;
ol { list-style-image:url(checkmark.jpg); }
/* Custom Bullet points */
p { }
/* Type - selects all elements of specified type (e.g., <p>)*/
* { }
/* Universal - selects all elements in html file not selected by other selectors */
#myID { }
/* ID - are supposed to singular and unique to allow you format a specific element */
.class { }
/* Class – similar to ID but for formatting multiple elements at same time Note HTML elements can have multiple classes */
h1, h2, #myID { }
/* Compound - format multiple selectors at same time */
body article p { }
/* Descendent - element that is nested inside of specified parent element */
a:link
/* Pseudo class - links in their default state before visitor has interacted with them */
a:visited
/* Links after user has already clicked on them and is visiting that page again */
a:hover
/* Links when the user is hovering their mouse over the link */
a:active
Margin: 40px 0px 0px 0px;
/* 40px on top, 0 on right, 0 on bottom, 0 on left */
Margin: 0 auto;
/* Zero margin on top and bottom; auto margin on left/right */
Margin-top:40px;
border-width: 5px;
/* sets border width to 5px */
border: solid;
/* border style */
border-color: black;
border-top-color: green;
border: 2px dashed;
/* border width and border style */
border: 10px ridge rgba(0,0,0,.5);
/* border width, style and color */
border-radius:12px;
/* makes the borders curved rather than square */
border-radius:20%;
padding:10px; puts 10px
/* padding on all sides */
padding:0px 10px 0px 10px;
/* Padding on top, right, bottom, left (i.e. it goes clockwise) */
padding-top:20px;
padding:0px 10px;
/* Padding on vertical, horizontal */
display:block;
/* Makes an inline element act like an block element */
display:inline-block;
/* Flows like inline element but can use properties of block element */
display:none;
/* hides the element */
/* only works on block level elements!! Not in-line! */
display: inline;
/* Makes a block element act like an inline element */
box-sizing: “content-box”;
/* specifies size of content box (margin, border, padding added on top of content); default setting */
box-sizing: “Border-box”;
/* specifies size of entire element (margin + border + padding + content); good to use to avoid mistakes */
# div1 {position:absolute; top:2px; left:2px; }
/* Absolute positioning allows you to control exactly where div is based on top left 0 position */
#div1 {position:fixed; top:2px; left:2px; }
/* Fixed positioning – similar to absolute positioning but doesn’t move when user scrolls */
# div1 {position:relative; top:2px; left:2px; }
/* Relative positioning – instead of using the origin and then moving it absolutely, it starts at where top left position of element would be without adjustments; this can be tricky to use */
<main> <div id=”1”> </div> <div id=”2”> </div> </main>
main {display:flex;}
#div1 {width:60%;}
#div2 {width:40%;}
main {justify-content:flex-start;}
/* Items are positioned horizontally at left of container; flex-end, center, space-around, space-between */
main {align-items:flex-start;}
/* Items are positioned vertically at bottom of container; flex-end, center, stretch, baseline */
@media screen and (min-width: 480px) {body {background: red;}}
/* sets background to red when screen size is more than 480px wide */
<meta name=”viewport” content=”width=device-width”, initial-scale=1”>
letter-spacing:0.09em;
/* em inherits either default font size or font size of parent; 1em = parent size; 2em = 2x parent’s size */
margin: 0 0 1.5em 0;
font-size: 2em;
overflow:hidden;
/* Hide content that overflows (e.g., nav bar) */
$(‘CSSselector’).action()
$(‘button’).click(color_switcher);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</body>
<script src="js/myscript.js"></script>
$(document).ready(function() {
//Put all of your code in here
});
$(‘*’).click(color_switcher)
// all elements
$(‘h2’).click(color_switcher)
// all <h2>’s
$(‘#big_box’).click(color_switcher)
// elements with id big_box
$(‘.important’).click(color_switcher)
$(‘this’).val();
// element that is currently being selected
$(‘p, h1’).click(color_switcher)
// all <p>’s and <h1>’s
$(‘#sidebar a’).click(color_switcher)
// <a>’s that are within sidebar id
$(‘p > a’).click(color_switcher)
// <a>’s that are direct children of <p>
$(‘div.container’).click(color_switcher)
// all <div>’s with class container
$('#check_boxes').change(prog1);
when check_boxes is changed (i.e. checked / unchecked), then run prog1
$('#id').click(edit_program);
when id is clicked, then edit_program is run
x = $(‘#id’).val();
get value of element and set x to it; can also set element values
$(‘#id’).text("Changes h1 text to this”);
change html text
$(‘#id’).css("background-color": "yellow");
change CSS attribute
$(‘#id’).attr(‘class’,’important’);
add ‘important’ class to id
$(‘#id’).append(“<option> choice 4 </option>”);
adds option html code to id
$(‘#id’).remove();
deletes html code to id
$('#id').on('click', '.btn-delete', delete_program);
similar to .click() method but used after you use .append() to add html
$(‘#id’).css({"background-color": "yellow", "font-size": "200%"});
change multiple CSS attributes in one line of code
$(‘#id’).hide();
hides element
$(‘#id’).show();
shows element
$(‘#id’).slideUp();
hides element by sliding it up
$(‘#id’).slideDown();
shows element by sliding it down
$(‘#id’).slideToggle();
toggles between .slideUp() and .slideDown() methods
$(‘#id’).toggleClass();
toggles between adding/removing one or more class names
$(‘#id’).attr(“src”, “http://lorempixel.com/g/500/400/food”); // changes the source attribute picture to a different picture
lorempixel.com/g/500/400/food”);
changes the source attribute picture to a different picture
$(‘#id’).children();
returns all direct children of the selected element
$(‘#id’).parent();
returns the direct parent of the selected element
$(‘S#id’).html(“<span> new text </span>”);