😄 Holaa! Esta es mi solución! Gracias a todos porque gracias a sus aportes resolví el reto!!
JS
const URL = 'https://api.thecatapi.com/v1/images/search';
async function myCat () {
const res = await fetch(URL);
const data = await res.json();
const img = document.querySelector('img');
img.src = data[0].url;
}
const myButton = document.querySelector("button");
myButton.onclick = myCat;
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./styles.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Pacifico&family=Square+Peg&display=swap" rel="stylesheet">
<title>Random Cats with API REST</title>
</head>
<body>
<header>
<h1>Random Cats</h1>
</header>
<section class="container">
<img alt="Cat random Picture" class="image">
<button type="button" class="random-cat-button"> Show me a cat!</button>
</section>
<script src="./main.js"></script>
</body>
</html>
CSS
* {
box-sizing: border-box;
margin: 0px;
padding: 0px;
}
html {
font-size: 62.5%;
}
body {
font-family: 'Square Peg', cursive;
}
header {
display: flex;
justify-content: center;
}
h1 {
font-size: 8rem;
}
.container {
display: flex;
flex-direction: column;
justify-content: center;
place-items: center;
padding: 20px;
}
.container .image {
border-radius: 20px;
margin: 20px;
width: auto;
height: 500px;
}
.random-cat-button {
align-items: center;
background-color: rgb(228, 164, 94);
border: 5px solid rgb(249, 151, 66);
border-radius: 15px;
cursor: pointer;
font-family: 'Pacifico', cursive;
font-size: 2rem;
padding: 12px;
width: 200px;
height: auto;
}
.random-cat-button:hover {
border: 5px solid rgb(228, 164, 94);
background-color: rgb(227, 141, 67);
}
¿Quieres ver más aportes, preguntas y respuestas de la comunidad?