APIs
This commit is contained in:
97
Practice/APIs/index.html
Normal file
97
Practice/APIs/index.html
Normal file
@ -0,0 +1,97 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Pokémon Info</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
background-color: #f2f2f2;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
}
|
||||
input[type="text"] {
|
||||
padding: 8px;
|
||||
width: 200px;
|
||||
border: 2px solid #4caf50;
|
||||
border-radius: 5px;
|
||||
margin-right: 10px;
|
||||
}
|
||||
input[type="button"] {
|
||||
padding: 8px 15px;
|
||||
background-color: #4caf50;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 5px;
|
||||
cursor: pointer;
|
||||
}
|
||||
input[type="button"]:hover {
|
||||
background-color: #45a049;
|
||||
}
|
||||
#pokemonweight {
|
||||
font-size: 1.2em;
|
||||
margin-top: 15px;
|
||||
color: #333;
|
||||
}
|
||||
#pokeimage {
|
||||
margin-top: 10px;
|
||||
width: 150px;
|
||||
height: 150px;
|
||||
border-radius: 10px;
|
||||
border: 3px solid #333;
|
||||
object-fit: cover;
|
||||
}
|
||||
h3 {
|
||||
color: #4caf50;
|
||||
margin-top: 30px;
|
||||
}
|
||||
ul {
|
||||
list-style-type: none;
|
||||
padding: 0;
|
||||
font-size: 1em;
|
||||
}
|
||||
ul li {
|
||||
background-color: #e0f7fa;
|
||||
margin: 5px 0;
|
||||
padding: 8px;
|
||||
border-radius: 5px;
|
||||
text-align: center;
|
||||
transition: background-color 0.3s;
|
||||
}
|
||||
ul li:hover {
|
||||
background-color: #b2ebf2;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<input type="text" name="" id="pokemon" placeholder="Enter pokemon name">
|
||||
<input type="button" value="submit" id="submit" onclick="pokemonapi()">
|
||||
<p id="pokemonweight"></p>
|
||||
<img src="" alt="" id="pokeimage">
|
||||
|
||||
<h3>Possible pokemon names</h3>
|
||||
<ul>
|
||||
<li>Bulbasaur</li>
|
||||
<li>Charmander</li>
|
||||
<li>Squirtle</li>
|
||||
<li>Pikachu</li>
|
||||
<li>Meowth</li>
|
||||
<li>Psyduck</li>
|
||||
<li>Golduck</li>
|
||||
<li>Poliwag</li>
|
||||
<li>Poliwhirl</li>
|
||||
<li>Poliwrath</li>
|
||||
<li>Abra</li>
|
||||
<li>Kadabra</li>
|
||||
<li>Alakazam</li>
|
||||
<li>Machop</li>
|
||||
<li>Machoke</li>
|
||||
<li>Machamp</li>
|
||||
</ul>
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
</html>
|
46
Practice/APIs/script.js
Normal file
46
Practice/APIs/script.js
Normal file
@ -0,0 +1,46 @@
|
||||
//Accessing API with fetch function
|
||||
|
||||
fetch("https://pokeapi.co/api/v2/pokemon/pikachu")
|
||||
.then(response =>{
|
||||
if(!response.ok){
|
||||
throw new Error("Not a pokemon!");
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => console.log(data.weight))
|
||||
.catch(error => console.error(error));
|
||||
|
||||
//Accessing API with async/await function
|
||||
|
||||
fetchData();
|
||||
|
||||
async function fetchData(){
|
||||
try{
|
||||
const response = await fetch("https://pokeapi.co/api/v2/pokemon/pikachu");
|
||||
if(!response.ok){
|
||||
throw new Error("Not a pokemon!");
|
||||
}
|
||||
const data = await response.json();
|
||||
console.log(data);
|
||||
}
|
||||
catch(error){
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
//FOR THE PAGE
|
||||
|
||||
async function pokemonapi(){
|
||||
const name = document.getElementById("pokemon").value.toLocaleLowerCase()
|
||||
try {
|
||||
const response = await fetch("https://pokeapi.co/api/v2/pokemon/" + name);
|
||||
if(!response.ok){
|
||||
throw new Error("Not a pokemon!");
|
||||
}
|
||||
const data = await response.json();
|
||||
document.getElementById("pokemonweight").innerText = "The weight of the pokemon is " + data.weight;
|
||||
document.getElementById("pokeimage").src = data.sprites.front_default;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
12
Practice/Json/Index.html
Normal file
12
Practice/Json/Index.html
Normal file
@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
</html>
|
1
Practice/Json/names.json
Normal file
1
Practice/Json/names.json
Normal file
@ -0,0 +1 @@
|
||||
["Spongebob", "Patrick", "Sandy", "Squidward"]
|
17
Practice/Json/people.json
Normal file
17
Practice/Json/people.json
Normal file
@ -0,0 +1,17 @@
|
||||
[{
|
||||
"name": "Spongebob",
|
||||
"age": 50,
|
||||
"isemployed": true
|
||||
}, {
|
||||
"name": "Patrick",
|
||||
"age": 32,
|
||||
"isemployed": true
|
||||
}, {
|
||||
"name": "Sandy",
|
||||
"age": 32,
|
||||
"isemployed": false
|
||||
}, {
|
||||
"name": "Squidward",
|
||||
"age": 32,
|
||||
"isemployed": false
|
||||
}]
|
10
Practice/Json/person.json
Normal file
10
Practice/Json/person.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"name": "Spongebob",
|
||||
"age": 50,
|
||||
"isemployed": true,
|
||||
"Hobbies": ["Karate", "Danceing", "Cooking"],
|
||||
"address": {
|
||||
"street": "Some st",
|
||||
"number": 123
|
||||
}
|
||||
}
|
19
Practice/Json/script.js
Normal file
19
Practice/Json/script.js
Normal file
@ -0,0 +1,19 @@
|
||||
const names = ["Spongebob", "Patrick", "Sandy", "Squidward"];
|
||||
|
||||
const jsonstrings = JSON.stringify(names);
|
||||
console.log(names);
|
||||
console.log(jsonstrings);
|
||||
|
||||
const person = {
|
||||
name: "Spongebob",
|
||||
age: 50,
|
||||
isemployed: true,
|
||||
Hobbies: ["Karate", "Danceing", "Cooking"],
|
||||
address: {
|
||||
street: "Some st",
|
||||
number: 123
|
||||
}
|
||||
};
|
||||
|
||||
console.log(person);
|
||||
console.log(JSON.stringify(person));
|
Reference in New Issue
Block a user