What could be improved in the following code?
let students = ["Bob","Amy","Betty","Bill","Marge","Charlotte","Shawn","Pedro"];
let longestName = function(){
let output = "";
for(let i = 0; i < students.length; i++){
let name = students[i];
if(name.length > output.length){
output = name;
}
}
return output;
}
longestName();
// preference for const
const students = ["Bob","Amy","Betty","Bill","Marge","Charlotte","Shawn","Pedro"];
// Keep array reference within function as parameter (more versatile)
const longestStr = function(arr){
let output = "";
for(let i = 0; i < arr.length; i++){
const str = arr[i];
output = str.length > output.length ? str : output;
}
return output;
}
longestStr(students); // must pass data into fun ction as argument
What is the value of pikachu.health?
const pikachu = {
health: 10,
defense: 5
}
const charmander = {
punch: function(obj){
const damage = 12 - obj.defense;
obj.health -= damage;
}
}
charmander.punch(pikachu);
pikachu.health; // (10 - (12 - 5)) == (10 - 7) == 3
How would you get all divs with a class of "xyz" from the DOM?
const divs = document.querySelectorAll("div.xyz");
Why does the following console.log() print "undefined?"
const URL = "https://some-api.com/api";
let info;
fetch(URL)
.then(r=>r.json())
.then(data=>{
info = data;
})
console.log(info); // undefined
The asynchronous fetch() call does not reassign the "info" variable before the console.log() executes
An events is some notable moment for an element, such as when a user:
Check out all of the events that can occur on a website
An event handler is a callback function that can be assigned to an element to trigger when an event occurs
const button = document.querySelector("button");
button.addEventListener("click",handleClick);
function handleClick(event){
// event is an Event object with information about the event
alert("Don't press me!");
}
In the "mainProject" folder, go into "class_4", and edit the "script.js" file
const button = document.querySelector("button");
button.addEventListener("click",handleClick);
function handleClick(event){
// event is an Event object with information about the event (check it out!)
alert("Don't press me!");
}
This is what we made!
Not all APIs are created equal - there are 3 main ways to access an API
Anyone can use these without needing permission or authentication!
const URL = "http://taco-randomizer.herokuapp.com/random/"; // TacoFancy API
fetch(URL)
.then(response=>response.json())
.then(console.log)
const BASE_API = "https://api.nasa.gov/planetary/apod"; // NASA API
const API_KEY = "YOUR KEY HERE"; // get API key by signing up as a developer
const URL = BASE_API + "?api_key=" + API_KEY;
// "?api_key=" sets a URL parameter (can test this URL in browser directly!)
// parameter name is specified in the documentation
fetch(URL)
.then(response=>response.json())
.then(console.log)
const URL = "https://favqs.com/api/qotd"; // Fav Quotes API
const API_KEY = "YOUR KEY HERE"; // get API key by signing up as a developer
const options = { // can have MANY more options
method:"GET", // GETting response instead of POSTing one
headers: {
"Content-Type" : "application/json", // specifying JSON as returning data type
"Authorization": "Token token=" + API_KEY // specified in documentation
}
}
fetch(URL, options) // notice 2nd parameter!
.then(response=>response.json())
.then(data=>console.log(data.quote))
API Documentation is written by developers to help other developers understand how to use their API
Unfortunately, there is no standard for writing documentation and you will often bump into hard-to-read, outdated, and cryptic documentation
Take 2 minutes to explore the Random User API Documentation
// 100 random users
"https://randomuser.me/api/?results=100"
// 100 female random users
"https://randomuser.me/api/?results=100&gender=female"
// a male from the US
"https://randomuser.me/api/?gender=male&nat=us"
// 10 random users with only their name and cell number
"https://randomuser.me/api/?results=10&inc=name,cell"
A typical pattern you'll see is when the data from one API call provides the information or setup for another API call.
// Random User API
const URL_1 = "https://randomuser.me/api";
// Sunrise and Sunset times API
const URL_2_BASE = "https://api.sunrise-sunset.org/json?date=today";
fetch(URL_1)
.then(response=>response.json())
.then(userData=>{
const name = userData.results[0].name;
const full_name = `${name.first} ${name.last}`;
const coordinates = userData.results[0].location.coordinates;
const lat = coordinates.latitude;
const long = coordinates.longitude;
// Sunrise & Sunset API can accept URL parameters for longitude
// and latitude to get the sunrise and sunset for that location
const URL_2 = URL_2_BASE + "&lat=" + lat + "&lng=" + long;
fetch(URL_2)
.then(response=>response.json())
.then(ss_data=>{
const sunrise = ss_data.results.sunrise;
const sunset = ss_data.results.sunset;
console.log("My name is" + full_name);
console.log("My sunrise was at " + sunrise);
console.log("My sunset will occur at " + sunset);
})
})
Notice the 2nd fetch is WITHIN the first's callback
Find 1 API we didn't use and create a web application allowing users to interact with it. For example:
The possiblities are endless!