//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);
    }
}