30 days of Code HackerRank-Day 4 Class Vs Instance
I’m doing the 30 Days of Code on HakerRank.com. I have not been keeping up every day since I just started on it but I would like to put in the effort to do so at least two to four times a week, if possible.
I want to do this because I want to get better at doing Data Structures and Algorithms and this seems to be a good, consistent way to do get better. And there seems to be a time table to this course, so I am up for it!
Today I’m on the Day 4: Class vs. Instance.
This challenge is categorized as Easy, Max Score is 30, and the Success Rate is 96.64%.
The Objective is:
“In this challenge, we’re going to learn about the difference between a class and an instance; because this is an Object Oriented concept, it’s only enabled in certain languages.”
The Task is:
“Write a Person class with an instance variable, age, and a constructor that takes an integer, initialAge, as a parameter. The constructor must assign initialAge to age after confirming the argument passed as initialAge is not negative; if a negative argument is passed as initialAge, the constructor should set age to 0 and print Age is not valid, setting age to 0..
In addition, you must write the following instance methods:
1. yearPasses() should increase the instance variable by 1.
2. amIOld() should perform the following conditional actions:
· If age < 13 , print You are young..
· If age ³ 13 and age < 18, print You are a teenager..
· Otherwise, print You are old..
To help you learn by example and complete this challenge, much of the code is provided for you, but you’ll be writing everything in the future. The code that creates each instance of your Person class is in the main method. Don’t worry if you don’t understand it all quite yet!
Note: Do not remove or alter the stub code in the editor.”
Ok, so here is what is provided in the text editor on the website for JavaScript:
Everything in RED is hidden
process.stdin.resume();
process.stdin.setEncoding(‘ascii’);
var input_stdin = “”;
var input_stdin_array = “”;
var input_currentline = 0;
process.stdin.on(‘data’, function (data) {
input_stdin += data;
});
process.stdin.on(‘end’, function () {
input_stdin_array = input_stdin.split(“\n”);
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
Everything is BOLD is what is worked on.
function Person(initialAge){
// Add some more code to run some checks on initialAge
this.amIOld=function(){
// Do some computations in here and print out the correct statement to the console
};
this.yearPasses=function(){
// Increment the age of the person in here
};
}
function main() {
var T=parseInt(readLine());
for(i=0;i<T;i++){
var age=parseInt(readLine());
var p=new Person(age);
p.amIOld();
for(j=0;j❤;j++){
p.yearPasses();
}
p.amIOld();
console.log(“”);
}
}
Let’s work on the comment:
// Add some more code to run some checks on initialAge
// If the initalAge is greater than 0, then set this.age to the initialAge
// If the initalAge is not greater than 0, then set this.age to 0 and console.log(“Age is not valid, setting age to 0.”)
if (initialAge > 0)
{ this.age = initialAge;
else {
this.age = 0;
console.log(“Age is not valid, setting age to 0.”);
}}
Let’s work on the next comment:
// Do some computations in here and print out the correct statement to the console
// if this.age is less than 13, then console.log(“You are young.”)
// if this.age is less than 18, then console.log(“You are a teenager.”)
// if this.age is greater than 18, then console.log(“You are old.”)
if (this.age < 13) console.log(“You are young.”);
else if (this.age < 18) console.log(“You are a teenager.”);
else console.log(“You are old.”);
and now to work on the last comment:
// Increment the age of the person in here
// to increment this.age all you have to do is add in two plus signs ++
this.age++
Here is the code altogether:
function Person(initialAge){
// Add some more code to run some checks on initialAge
if (initialAge > 0) this.age = initialAge;
else {
this.age = 0;
console.log(“Age is not valid, setting age to 0.”);
}
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 < 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++
};
}
All in all, this was easy.
I’m glad I started doing the 30 Days of Code on HakerRank.com.
I really hope I can do at it at least two to four times a week because, as stated before, I do want to get better at solving problem.
If you want to start on the 30 Days of Code on HakerRank.com just click on the underlined and highlighted text on this sentence. I’m sure you’ll enjoy it like I did and you’ll have learned something new.