Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda