Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidystephenson/2abcfada5587dea66598c9182812950c to your computer and use it in GitHub Desktop.
Save davidystephenson/2abcfada5587dea66598c9182812950c to your computer and use it in GitHub Desktop.
// Type definition for wand using tuple
type Wand = [core: string, length: number, material: string]
// Enum for house names
enum House {
Gryffindor = 'Gryffindor',
Slytherin = 'Slytherin'
}
// Type for character
interface Character {
name: string
age: number
isWizard: boolean
house: House
spells: string[]
wand: Wand
}
// Array to store all characters
const characters: Character[] = []
// Function to add a new character
function addCharacter (character: Character) {
characters.push(character)
}
// Function to display all characters
function displayCharacters () {
characters.forEach(character => {
console.log('Name:', character.name)
console.log('Age:', character.age)
console.log('Wizard:', character.isWizard)
console.log('House:', character.house)
console.log('Spells:', character.spells)
console.log('Wand Core:', character.wand[0])
console.log('Wand Length:', character.wand[1])
console.log('Wand Material:', character.wand[2])
console.log('\n')
})
}
// Function to filter characters by house
function filterByHouse (house: House) {
return characters.filter(character => {
return character.house === house
})
}
// Function to count wizards vs muggles
function countByMagicalStatus () {
const summary = { wizards: 0, muggles: 0 }
characters.forEach(character => {
if (character.isWizard) {
summary.wizards += 1
} else {
summary.muggles += 1
}
})
return summary
}
// Sample data
addCharacter({
name: "Harry Potter",
age: 17,
isWizard: true,
house: House.Gryffindor,
spells: ["Expelliarmus", "Expecto Patronum"],
wand: ["Phoenix Feather", 11, "Holly"]
});
addCharacter({
name: "Hermione Granger",
age: 17,
isWizard: true,
house: House.Gryffindor,
spells: ["Alohomora", "Petrificus Totalus"],
wand: ["Dragon Heartstring", 10.75, "Vine"]
});
addCharacter({
name: "Dudley Dursley",
age: 18,
isWizard: false,
house: House.Slytherin,
spells: [],
wand: ["None", 0, "None"]
});
// Displaying characters
displayCharacters()
// Filter by house
console.log("\n Characters from Gryffindor:");
console.log(filterByHouse(House.Gryffindor));
// Magical status summary
const summary = countByMagicalStatus();
console.log(`\n Wizards: ${summary.wizards}, Muggles: ${summary.muggles}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment