Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!