Link to home
Start Free TrialLog in
Avatar of Dung Trinh
Dung Trinh

asked on

Javascript question

I am new to javascript and in one of my lecture, there is one question that I couldn't solve or understand how to solve it. I have to fill in the code in Sample in order to have a matching output. I tried to create
mostRecentSample = new Sample(first, last);

Open in new window

but it cause too much recursion error. Any suggestion? If you can come up with a solution, please explain why it work
var Sample = function(first, last) {
   //code in here
};

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

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

main();

Open in new window


/*Output
Doe, John
Doe, John
Smith, Jane
Smith, Jane
*/
ASKER CERTIFIED SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dung Trinh
Dung Trinh

ASKER

Thank you. Yes. the part ("In comparation") is put in just to debug.
I am taking a introduction to javascript class. I am using the book "Javascript, The Good part."