We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
TypeScript solution. Note, I added an ageBy helper method to the Person class for to DRY up duplicate aging code :
functionmain(){// Enter your code hereconsttestAmount=Number(readLine())constlastTestIndex=4Array.from({length:testAmount}).forEach((_,index)=>{constp1=newPerson(Number(readLine()))p1.amIOld()p1.ageByYears(3)p1.amIOld()if(index!==lastTestIndex)process.stdout.write('\n')})}classPerson{age=0constructor(initalAge:number){if(initalAge<0){process.stdout.write('Ageisnotvalid,settingageto0.\n')return}this.age=initalAge}yearPasses():void{this.age++}amIOld():void{if(this.age<13){process.stdout.write('Youareyoung.\n')}elseif(this.age>=13&&this.age<18){process.stdout.write('Youareateenager.\n')}else{process.stdout.write('Youareold.\n')}}ageByYears(years:number){Array.from({length:years}).forEach(_=>this.yearPasses())}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 4: Class vs. Instance
You are viewing a single comment's thread. Return to all comments →
TypeScript solution. Note, I added an
ageByhelper method to the Person class for to DRY up duplicate aging code :