Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My job is to build a constructor with print and mostRecent but I don\'t know how

ID: 3570988 • Letter: M

Question

My job is to build a constructor with print and mostRecent but I don't know how to make mostRect equals to sample so it can print out for the second time

var Sample = function(first, last) {
this.first = first;
this.last = last;

this.print = function() {
print(this.last + ", " + this.first);
}

//this.mostRecent = Sample;
};

var main = function() {
var sample = new Sample("John", "Doe");

sample.print();   
Sample.print();
if (Sample.mostRecent === sample) {
Sample.mostRecent.print();
}

sample = new Sample("Jane", "Smith");
sample.print();
if (Sample.mostRecent === sample) {
Sample.mostRecent.print();
}
};

main();

Explanation / Answer

var Sample = function(first, last) {
this.first = first;
this.last = last;

this.print = function() {
print(this.last + ", " + this.first);
}

this.mostRecent = this;
};

var main = function() {
var sample = new Sample("John", "Doe");

sample.print();   
Sample.print();
if (Sample.mostRecent === sample) {
Sample.mostRecent.print();
}

sample = new Sample("Jane", "Smith");
sample.print();
if (Sample.mostRecent === sample) {
Sample.mostRecent.print();
}
};

main();

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote