Skip to content

Instantly share code, notes, and snippets.

@davidystephenson
Created September 27, 2025 18:54
Show Gist options
  • Save davidystephenson/828ea1a21ff20ce5bb6d15e5a3c46304 to your computer and use it in GitHub Desktop.
Save davidystephenson/828ea1a21ff20ce5bb6d15e5a3c46304 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 () {
for (const character of characters) {
const wizard = character.isWizard ? 'wizard' : 'muggle'
const reversed = character.spells.reverse()
const [last, ...first] = reversed
const firstReversed = first.reverse()
const firstJoined = firstReversed.join(', ')
const spellsConjuction = first.length > 1 ? ', and' : ' and'
const hasSpells = `${firstJoined}${spellsConjuction} ${last}`
const spells = character.spells.length > 1 ? `the spells ${hasSpells}` : 'no spells'
const [core, length, material] = character.wand
const wand = length > 0
? `whose wand has a ${core} core, a length of ${length}, and a ${material} material`
: 'who has no wand'
const message = `${character.name} is a ${character.age} year old ${wizard} from house ${character.house} with ${spells} ${wand}!`
console.log(message)
}
}
// Function to filter characters by house
function filterByHouse (house: House) {
return characters.filter(character => character.house === house)
}
// Function to count wizards vs muggles
function countByMagicalStatus () {
const wizards = characters.filter(character => character.isWizard)
const summary = {
wizards: wizards.length,
muggles: characters.length - wizards.length
}
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