Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
Using switch to display response to a number from the API

I have made a webpage that returns the age of a person based on their name.

I decided to use a switch statement to display a message based on the age returned from the API but it will not display on the webpage. Any help is appreciated!

const btn = document.querySelector(".btn"); //Button to initiate API request

const getAge = async (name) => {
  const response = await fetch(`https://api.agify.io?name=${name}`); //API url used to request age after name is submitted
  const data = await response.json();
  console.log(data);
  
  const header = document.querySelector('.header'); //Response from API is displayed after message
  const message =`Your Age is:`;
  header.textContent = message;

  const content = document.querySelector('.content'); //Actual age returned from API
  content.textContent = data.age;
};

btn.addEventListener("click", (e) => { //Event listening for button click
  e.preventDefault;
  const name = document.querySelector(".input-item").value;
  getAge(name); 
});

var a = content;

switch(data.age(a)) {
  case a<=18:
    document.write("Just a youngin!") //if e<=18
    break;
  case a>=19 && a<40:
    document.write("Mid-life crisis incoming!") //if e>=19 && e<=40
    break;
  case a>=41:
    document.write("Too Old!") //if e>=41
    break;
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda