DEV Community

Cover image for HackerRank 30 Days of Code Solution Day 4: Class vs. Instance
Aayushi Sharma
Aayushi Sharma

Posted on • Originally published at codeperfectplus.com on

HackerRank 30 Days of Code Solution Day 4: Class vs. Instance

In this tutorial, we will guide you through the process of solving the Day 4: Class Vs. Instance programming problem from HackerRank’s “ 30 Days of Code ” challenge.

We will provide comprehensive code solutions in C++, Python, and JavaScript programming languages. With our help, you will gain a better understanding of the problem and learn how to approach and solve similar coding challenges.

Disclaimer: We encourage you to solve this challenge yourself before reading our tutorial. We have provided a detailed explanation of the problem and our solutions to help you check your work.

Hackerrank 30 days of code - Day 4: Class vs. Instance

It’s part of Hackerank’s 30 days of code. A series of 30-day programming challenges. Where you can learn new programming languages by solving the coding challenges.

Problem Statement and Explanation

In this problem, we will learn the difference between class and instance using object-oriented programming. we will return the following string based on the following conditions:

  • If the age is 0 or less, we will print Age is not valid, setting age to 0. and set the age to 0.
  • There are 3 conditions to check the age:
    • If the age is less than 13, we will print You are young.
    • If the age is greater than or equal to 13 and less than 18, we will print You are a teenager.
    • Otherwise, we will print You are old.
  • There will be two methods:
    • amIOld() method to check if the person is old or not.
    • yearPasses() method to increment the age of the person.

The Person class is provided in the editor. We have to write the following methods inside the class:

Operators Hackerrank solution in Python

class Person:
def __init__(self, initialAge):
# Add some more code to run some checks on initialAge
if initialAge < 0:
print("Age is not valid, setting age to 0.")
self.age = 0
else:
self.age = initialAge
def amIOld(self):
if self.age < 13:
print("You are young.")
elif 13 <= self.age < 18:
print("You are a teenager.")
else:
print("You are old.")
# Increment the age of the person in here
def yearPasses(self):
# Increment the age of the person in here
self.age += 1

Operators Hackerrank solution in JavaScript

function Person(initialAge) {
// Add some more code to run some checks on initialAge
if (initialAge < 0) {
console.log("Age is not valid, setting age to 0.");
this.age = 0;
} else {
this.age = initialAge;
}
this.amIOld = function () {
// Do some computations in here and print out the correct statement to the console
if (this.age < 13) {
console.log('You are young.');
} else if (this.age >= 13 && this.age < 18) {
console.log("You are a teenager.");
} else {
console.log('You are old.');
}
};
this.yearPasses = function () {
// Increment the age of the person in here
this.age += 1;
};
}

Test Case of Class vs. Instance Hackerrank Solution

Tested on the Hackerrank platform for the following test case and passed all the test cases. Enclosed below is the screenshot of the test case.

class vs instance

Problem statement is taken from Hackerrank, and the solutions are implemented by CodePerfectPlus team

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)