Skip to content
Permalink
e73841c288
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
53 lines (50 sloc) 1.38 KB
const petsData = [
{
petName: "Stella",
age: 7,
weightInKilos: 24,
breed: "Dalmation"
},
{
petName: "Cody",
age: 8,
weightInKilos: 22,
breed: "Corgi"
},
{
petName: "Mango",
age: 2,
weightInKilos: 11,
breed: "Persian"
},
{
petName: "Lucy",
age: 4,
weightInKilos: 35,
breed: "Ball Python"
},
{
petName: "Buhmie",
age: 1,
weightInKilos: 28,
breed: "Bull-dog"
}
];
function showInfo() {
const petNum = document.getElementById('petNum').value;
const petIndex = parseInt(petNum) - 1;
const infoElement = document.querySelector('.selectedPetInfo');
if (petIndex >= 0 && petIndex < petsData.length) {
const pet = petsData[petIndex];
const petType = getPetType(pet.breed);
infoElement.textContent = `${pet.petName} the ${petType} is ${pet.age} years old. This ${petType} weighs ${pet.weightInKilos} kilos and is a ${pet.breed} breed.`;
} else {
infoElement.textContent = 'Invalid pet number. Please enter a number between 1 and ' + petsData.length + '.';
}
}
function getPetType(breed) {
const lowerBreed = breed.toLowerCase();
if (lowerBreed.includes('persian')) return 'cat';
if (lowerBreed.includes('python')) return 'snake';
return 'dog';
}