What is wrong with the following function?
function factorial(n) => {
if (n===1) {
return 1;
}
return n * factorial(n-1);
}
// It's trying to combine 2 different ways of defining a function
// Could go with function declaration...
function factorial(n) {
if (n===1) {
return 1;
}
return n * factorial(n-1);
}
// ...or arrow function
const factorial = n => {
if (n===1) {
return 1;
}
return n * factorial(n-1);
}
How would you access the 2nd item in the object's array?
const superman = {
name: "Kal-El",
alter_ego: "Clark Kent",
origin: "Krypton",
powers: ["strength","x-ray vision","indestructibility","ice breath","eye beam"]
}
superman.powers[1]; // x-ray vision
I can't see the <p> element I added to the DOM. Here's my "script.js" file. Why isn't it working?😭
const container = document.querySelector(".container");
const p = document.createElement("p");
p.innerHTML = "Hello World!";
// Need to attach/append the newly create element to a DOM element
container.append(p);
I made the change - my JavaScript is correct, but I still can't see the <p> element on the website. Here's my "index.html" file. Why isn't it working?😭
<html>
<head>
<title>Cool Site</title>
</head>
<body>
<div class="container"></div>
</body>
</html>
<html>
<head>
<title>Cool Site</title>
<!--Forgot to load the JavaScript file!-->
<script src="script.js"></script>
</head>
<body>
<div class="container"></div>
</body>
</html>
JavaScript executes each line of code from top to bottom
const name = "Bob";
const age = 42;
console.log(name); // Bob
console.log(age); // 42
This is a common and expected behavior of code execution
There are instances when code will get executed, but NOT resolve immediately, such as requesting information from an API server
const URL = "https://randomuser.me/api";
let data;
fetch(URL) // Requesting JSON from API (like $.ajax)
.then(response=>response.json())
.then(json=>{
data=json;
})
console.log(data); // undefined
During code execution, the fetch() call sends a request to a URL, but that could take a long time (~6ms)! JavaScript is not going to wait - it will move onto the console.log() and let callbacks handle the response
const URL = "https://randomuser.me/api";
let data;
fetch(URL) // Requesting JSON from API (like $.ajax)
.then(response=>response.json())
.then(json=>{
data=json;
})
console.log(data); // undefined
API stands for Application Program Interface
Example JSON from Random User API
{"results":[{"gender":"female","name":{"title":"ms","first":"carolyn","last":"brooks"},"location":{"street":"5508 king street","city":"wakefield","state":"warwickshire","postcode":"M0 5UX","coordinates":{"latitude":"14.0309","longitude":"165.8724"},"timezone":{"offset":"+8:00","description":"Beijing, Perth, Singapore, Hong Kong"}},"email":"carolyn.brooks@example.com","login":{"uuid":"31cd84ee-8a01-4254-b322-675ef00de0fa","username":"bluegorilla497","password":"birthday","salt":"Nbhjusmn","md5":"6bc053eccfdc3078acab5d96b5278f0f","sha1":"371147ed185f134a65e0f201b0f82b5e55ff2223","sha256":"60fc47a547217783428ffc170709adc8bad0efa5404fff41282c57d9b37c33ba"},"dob":{"date":"1962-08-29T20:41:30Z","age":56},"registered":{"date":"2016-12-08T04:51:11Z","age":2},"phone":"028 3843 5424","cell":"0770-555-954","id":{"name":"NINO","value":"LH 29 72 52 O"},"picture":{"large":"https://randomuser.me/api/portraits/women/95.jpg","medium":"https://randomuser.me/api/portraits/med/women/95.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/95.jpg"},"nat":"GB"}],"info":{"seed":"ed31d9a21fc2e60e","results":1,"page":1,"version":"1.2"}}
Backup JSON from Random User API - just in case!
{"results":[{"gender":"female","name":{"title":"ms","first":"carolyn","last":"brooks"},"location":{"street":"5508 king street","city":"wakefield","state":"warwickshire","postcode":"M0 5UX","coordinates":{"latitude":"14.0309","longitude":"165.8724"},"timezone":{"offset":"+8:00","description":"Beijing, Perth, Singapore, Hong Kong"}},"email":"carolyn.brooks@example.com","login":{"uuid":"31cd84ee-8a01-4254-b322-675ef00de0fa","username":"bluegorilla497","password":"birthday","salt":"Nbhjusmn","md5":"6bc053eccfdc3078acab5d96b5278f0f","sha1":"371147ed185f134a65e0f201b0f82b5e55ff2223","sha256":"60fc47a547217783428ffc170709adc8bad0efa5404fff41282c57d9b37c33ba"},"dob":{"date":"1962-08-29T20:41:30Z","age":56},"registered":{"date":"2016-12-08T04:51:11Z","age":2},"phone":"028 3843 5424","cell":"0770-555-954","id":{"name":"NINO","value":"LH 29 72 52 O"},"picture":{"large":"https://randomuser.me/api/portraits/women/95.jpg","medium":"https://randomuser.me/api/portraits/med/women/95.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/95.jpg"},"nat":"GB"}],"info":{"seed":"ed31d9a21fc2e60e","results":1,"page":1,"version":"1.2"}}
json.results[0].cell;
json.results[0].login.username;
json.info.version;
A callback is a function that is passed into another function as an argument to be called later
const add = (a,b) => a + b;
add(1,3); // 4
const numbers = [1,2,3];
combineArray(numbers, add); // We'll define "combineArray" in a sec!
NOTE: when passing a callback, do NOT use parenthesis, which would immediately invoke the function
const add = (a,b) => a + b;
/**
* Combines each element of the given array
* using the given combiningFunction.
*/
const combineArray = (arr, combiningFunction) => {
let total = arr[0];
for(let i = 1; i < arr.length; i++){
total = combiningFunction(total, arr[i]); // callback called here
}
return total;
}
const numbers = [1,2,3];
combineArray(numbers, add); // 6
const stringify = (a,b) => String(a) + String(b);
/**
* Combines each element of the given array
* using the given combiningFunction.
*/
const combineArray = (arr, combiningFunction) => {
let total = arr[0];
for(let i = 1; i < arr.length; i++){
total = combiningFunction(total, arr[i]); // callback called here
}
return total;
}
const numbers = [1,2,3];
combineArray(numbers, stringify); // "123"
fetch() is a default function in the browser that can make HTTP requests
const URL = "https://randomuser.me/api";
fetch(URL);
const URL = "https://randomuser.me/api";
const handleResponse = response => {
// do something with response
}
fetch(URL).then(handleResponse);
const URL = "https://randomuser.me/api";
const handleResponse = response =>{
console.log(response);
}
fetch(URL).then(handleResponse);
Run the above code in the browser
const URL = "https://randomuser.me/api";
const handleResponse = response => response.json();
const handleJSON = json => console.log(json);
fetch(URL).then(handleResponse).then(handleJSON);
The "handleJSON" function should console.log() the same object you can see when going directly to https://randomuser.me/api in the browser
const URL = "https://randomuser.me/api";
let data;
fetch(URL)
.then(response=>response.json())
.then(json=>{
data = json; // setting data!
})
console.log(data); // undefined...wait, what!?
In the "mainProject" folder, go into "class_3", and edit the "script.js" file