Given the array "students", how would you access the last item?
const last = students[students.length-1];
What will the last expression evaluate to?
let gold = 100;
gold >= 120 ? "Sword acquired" : "{Not enough gold}";
"{Not enough gold}"
What Array method adds a given element to the end of the array?
.push()
const games = ["XCOM","Mario Kart","Destiny"];
games.push("Anthem");
console.log(games); // ["XCOM","Mario Kart","Destiny","Anthem"]
Describe what the following function "x" does
let x = () => Math.random() > .6 ? "Heads" : "Tails";
let x = function(){
return Math.random() > .6 ? "Heads" : "Tails";
}
let x = function(){
const randNum = Math.random();
if(randNum > .6){
return "Heads";
}else{
return "Tails";
}
}
Is this a "fair coin?" Why or why not?
An object is a common data structure containing properties (variables) and methods (functions)
const objectName = {
"key" : "value",
question : "What is the meaning of life?",
answer : function(){ return 42 }
}
const myCat = {
name : "Harry",
age : 4,
talk : function(){console.log("Meow!")},
state : "hungry"
}
myCat.name; // Harry
myCat["name"]; // Harry
myCat.state; // hungry
myCat.state = "full";
myCat.state; // full
myCat.talk(); // Meow!
Let's make a town with buildings, like a bank, and have inhabitants with bank accounts
const bob = {
name : "Bob Bobbington",
age : 45,
bank_account : {
number : "3230-2346-623",
amount : 502.56
}
}
const bank = {
accounts : [bob.bank_account, sam.bank_account, derek.bank_account],
location : "643 Broadway St. NY",
is_fdic_insured : true
}
const town = {
buildings : [bank, hospial, gas_station, library],
population : 48170,
inhabitants : [bob, sam, derek],
state : "NY"
}
Open up the console in the browser
const bob = {};
bob.age = 33;
bob.phrase = function(){return "Bob's your uncle!"};
JSON is one of the most common formats used then passing information over the internet
{
response: 200,
results: [
{
user : "bobbington",
password: "pa$$w0rd1!"
},
{
user : "sarah478",
password: "DROPTABLEusers;"
},
{
user : "rockinKIDD",
password: "qwerty123"
}
]
}
Notice the object stands alone - not assigned to a variable
[
{
name: "Boom Boom Pow",
artist: "The Black Eyed Peas",
year: 2009
},
{
name: "Moves like Jagger",
artist: "Maroon 5",
year: 2010
},
{
name: "Endless Love",
artist: "Lionel Richie",
year: 1981
}
]
In the "mainProject" folder, go into "class_2", and view the "index.html" file in the browser
// Access the question
questionObj.question
// Change the difficulty to hard
questionObj.difficulty = "hard";
// Create a new property, "image_url", and set it to "games.png"
questionObj.image_url = "games.png";
// Access the 2nd item in the array
questionObj.incorrect_questions[1]; // Ubisoft
// Add "PAC-MAN" to the array
questionObj.incorrect_questions.push("PAC-MAN");
How do we get this...
From this?
The browser translates HTML into JavaScript objects known as the Document Object Model
HTML
<html>
<head>
<title>My Site</title>
</head>
<body>
<h2>Some Title</h2>
<p>First paragraph</p>
<p>Second paragraph</p>
</body>
</html>
DOM
HTML
<html>
<head>
<title>My Site</title>
</head>
<body>
<h2 id="mainTitle">Some Title</h2>
<p>First paragraph</p>
<p>Second paragraph</p>
</body>
</html>
JavaScript
// document.querySelector returns first matching element found
const h2 = document.querySelector("#mainTitle");
// document.querySelectorAll returns an array of all matching elements found
const all_ps = document.querySelectorAll("p");
DOM nodes/elements are objects with their own properties that can be modified!
JavaScript
// document.querySelector returns first matching element found
const h2 = document.querySelector("#mainTitle");
h2.innerText = "Something else!";
h2.style.color = "red";
BEFORE
AFTER
JavaScript
// document.createElement() creates a new node of the given tag string
const newH3 = document.createElement("h3");
newH3.innerText = "Some text";
BEFORE
AFTER
The node is not attached to the DOM, therefore it won't be rendered!
Elements must be attached, or appended, to another element in the DOM to be rendered in the browser
// document.createElement() creates a new node of the given tag string
const newH3 = document.createElement("h3");
newH3.innerText = "Some text";
const body = document.querySelector("body"); // document.body
body.append(newH3);
Removing elements from the DOM is as easy as .remove()
newH3.remove();
In the "mainProject" folder, go into "class_2", and edit the "script.js" file
In the "mainProject" folder, go into "class_2", and edit the "script.js" file