Class 26 Flashcards

1
Q

Explain what’s going on here

fetch(“https://dog.ceo/api/breeds/image/random”) // api for the get request
.then(response => response.json())
.then(data => {
console.log(data)
document.querySelector(“img”).src = data.message})

A

Using fetch api to fetch data from dog.ceo api
. then() is a method is used to handle the response, converting it to JSON using the json() method. The second then() logs the retrieved data to the console, and display image (data.message) into my html document (into image tag)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does fetch() do in fetch API?

A

The fetch() method (function) starts the process of fetching a resource from the network, returning a promise that is fulfilled once the response is available.

The fetch() function is at the core of this API, and it takes one mandatory argument – the URL of the resource you want to fetch.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

For Dog API (random image), when I console log data, it logs {message: “…” status: “…”}. What does it mean?

A

It means data is an object with 2 properties (message and status)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is url?

A

Universal Resource Locator; It is an address which shows where to find a document or resource

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

For Dog API (random image), message property of data (data.message) is https://…jpg
What can I do with it?

A

data.message is an image url.
I can use this to display an image file.
Ex.
document.querySelector(“img”).src = data.message

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the syntax for selecting a random element from an array?
Given name of an array is array.

A

array[Math.floor(Math.random() * array.length)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does Math.random()
return?

A

Math.random() returns a random number between 0 (inclusive) and 1 (exclusive).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does Math.floor() do?

A

Math.floor() is used to round down the generated index to the nearest integer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are symbols of back-ticks?

A

Back-ticks are ...
Don’t get confused with single quotes ‘…’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are template literals used for and its syntax?

A
  1. multi-line strings
    - let text = ` Hi,
    How are
    you? ‘
  2. string interpolation
    - ${...}
  3. quotes inside strings (allow for both single and double quotes inside a string)
    - He's often called "Johnny"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Uncaught TypeError: Cannot read properties of null (reading ‘addEventListener’)” error in a basic JS button

A

Your code seems to be executed even before the DOM getting loaded into the browser.

Put your script import at the end of your HTML’s body

<html>
<body>
.........



</body>
</html>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is this error and how do you resolve it?
Access to fetch at ‘file:///Users…’ from origin ‘null’ has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.

A

I don’t know what kind of error this is but adding https:// before www in the url resolves this error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Look at this code and explain to me why method 1 and 2 work but not 3.

fetch(“https://www.thecocktaildb.com/api/json/v1/1/search.php?s=margarita”)
.then(response => response.json())
.then(data => {
console.log(data)
console.log(data.drinks[1].strDrink)

1. let x = data.drinks[1].strDrink
document.querySelector("p").innerText = x

2. document.querySelector("p").innerText = data.drinks[1].strDrink

3. data.drinks[1].strDrink = document.querySelector("p").innerText   })
A

For methods 1 and 2, you’re setting the inner text of the <p> element to the name of the cocktail (data.drinks[1].strDrink).

For method 3, you’re attempting to assign the value of the <p> element’s inner text to data.drinks[1].strDrink. However, this approach doesn’t work because you’re trying to modify the data object returned by the API. The data object is the response from the API, and it’s generally not a good practice to modify it directly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

fetch(“https://www.thecocktaildb.com/api/json/v1/1/search.php?s=margarita”)
.then(response => response.json())
.then(data => {
console.log(data)
data.drinks.forEach((object) => {
let p = document.createElement(“p”)
p.innerText = object.strDrink
document.body.appendChild(p)
})
})

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly