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;
}