• + 0 comments

    TypeScript solution. Note, I added an ageBy helper method to the Person class for to DRY up duplicate aging code :

    function main() {
        // Enter your code here
        
        const testAmount = Number(readLine())
        
        const lastTestIndex = 4
        
        Array.from({length: testAmount}).forEach((_, index) => {
            const p1 = new Person(Number(readLine()))
            p1.amIOld()
            p1.ageByYears(3)
            p1.amIOld()
            
            if (index !== lastTestIndex) process.stdout.write('\n')
        })
    }
    
    class Person {
        age = 0
        
        constructor(initalAge: number) {
            if (initalAge < 0) {
                process.stdout.write('Age is not valid, setting age to 0.\n')
                return
            }
            
            this.age = initalAge
        }
        
        yearPasses(): void {
            this.age++
        } 
        
        amIOld(): void {
            if (this.age < 13) {
                process.stdout.write('You are young.\n')
            } else if (this.age >= 13 && this.age < 18) {
                process.stdout.write('You are a teenager.\n')
            } else {
                process.stdout.write('You are old.\n')
            }
        }
        
        ageByYears(years: number) {
            Array.from({length: years}).forEach(_ => this.yearPasses())
        }
    }