Skip to content

Instantly share code, notes, and snippets.

@juliaamosova
Created September 29, 2017 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliaamosova/e86c53c78a53a5434a6379f4f93f79c5 to your computer and use it in GitHub Desktop.
Save juliaamosova/e86c53c78a53a5434a6379f4f93f79c5 to your computer and use it in GitHub Desktop.
Class vs. Instance Challenge
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++];
}
function Person(initialAge){
// Add some more code to run some checks on initialAge
if (initialAge < 0) {
initialAge = 0;
console.log("Age is not valid, setting age to 0.");
} else if (initialAge > 0) {
age = initialAge;
};
this.amIOld=function(age){
// Do some computations in here and print out the correct statement to the console
if (initialAge >=0 && initialAge < 13) {
console.log("You are young.");
} else if (initialAge >=13 && initialAge < 18) {
console.log("You are a teenager.");
} else if (initialAge >= 18) {
console.log("You are old.");
}
};
this.yearPasses=function(age){
// Increment the age of the person in here
initialAge += 1;
};
}
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<3;j++){
p.yearPasses();
}
p.amIOld();
console.log("");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment