Untitled
unknown
plain_text
9 months ago
1.4 kB
14
Indexable
function compareAges() {
// 1. Define Google's Age
const GOOGLE_AGE = 25; // Google was founded in 1998, making it 25 in 2023
// 2. Get the artist's age from the input field
// parseInt() converts the input value (which is a string) into an integer
const artistAge = parseInt(document.getElementById('artistAge').value);
// 3. Get the element where the result will be displayed
const resultElement = document.getElementById('result');
// 4. Validate the input (check if it's a number)
if (isNaN(artistAge) || artistAge <= 0) {
resultElement.textContent = "Please enter a valid, positive age for your artist.";
return;
}
// 5. Compare the ages and build the result message
let message = `Your artist is **${artistAge}** years old. Google is **${GOOGLE_AGE}** years old. `;
if (artistAge > GOOGLE_AGE) {
const difference = artistAge - GOOGLE_AGE;
message += `Your artist is **${difference}** years older than Google!`;
} else if (artistAge < GOOGLE_AGE) {
const difference = GOOGLE_AGE - artistAge;
message += `Your artist is **${difference}** years younger than Google!`;
} else {
message += `Wow! Your artist is the exact same age as Google!`;
}
// 6. Display the final message
resultElement.innerHTML = message;
}
Editor is loading...
Leave a Comment