-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
47 lines (36 loc) · 1.39 KB
/
index.js
File metadata and controls
47 lines (36 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { getComida } from "./request.js";
//INDEX
async function mostrarComida(nombreAlimento) {
const containerAlimentos = document.getElementsByClassName("comidas")[0];
// Eliminar contenido existente
while (containerAlimentos.firstChild) {
containerAlimentos.removeChild(containerAlimentos.firstChild);
}
const alimentos = await getComida(nombreAlimento);
const listaAlimentos = alimentos.foods;
listaAlimentos.forEach((alimento) => {
añadirComida(alimento);
});
}
function añadirComida(alimento) {
const containerAlimentos = document.getElementsByClassName("comidas")[0];
const gramos = 100;
const nombre = alimento.description;
const calorias = alimento.foodNutrients.find(
(nutriente) => nutriente.nutrientName === "Energy"
); // Buscar calorías
const comida = document.createElement("div");
comida.classList.add("div-comida");
comida.innerHTML = `<p>${nombre}</p> <p>${
calorias ? calorias.value : "No disponible"
} kcal <br> ${gramos} g </p>`;
containerAlimentos.appendChild(comida);
}
const btnBuscar = document.getElementsByClassName("btn-buscar")[0];
btnBuscar.addEventListener("click", () => {
const input = document.getElementsByClassName("buscador")[0];
const nombreAlimento = input.value;
const explorarH2 = document.getElementsByClassName("explorar")[0];
if (explorarH2) explorarH2.remove();
mostrarComida(nombreAlimento);
});