Challenge: Solution Review

This lesson will explain the solution to the problem from the previous coding challenge.

We'll cover the following...

Solution #

Press + to interact
function Assignment() {
this.make = function(builder){
builder.step1();
builder.step2();
builder.step3();
builder.step4();
return builder.get();
}
}
function AssignmentBuilder(subject,level,dueDate) {
this.assignment = null;
this.step1 = function() {
this.assignment= new Task();
};
this.step2 = function() {
this.assignment.addSubject(subject);
};
this.step3 = function(){
this.assignment.addLevel(level);
}
this.step4 = function(){
this.assignment.addDuedate(dueDate);
}
this.get = function() {
return this.assignment;
};
}
function Task() {
this.subject = null;
this.level = null;
this.dueDate = null;
this.addSubject = function(subject) {
this.subject = subject;
};
this.addLevel = function(level) {
this.level = level;
};
this.addDuedate = function(dueDate){
this.dueDate = dueDate;
}
this.announcement = function(){
console.log(`Your ${this.subject} assigment is: ${this.level}. It is due on ${this.dueDate}.`)
}
}
var assignment = new Assignment();
var assignmentBuilder = new AssignmentBuilder("Math","Hard","12th June, 2020");
var mathAssignment = assignment.make(assignmentBuilder);
mathAssignment.announcement();

Explanation

You were given the following code:

var assignment = new Assignment();
var assignmentBuilder = new AssignmentBuilder("Math","Hard","12th June, 2020");
var mathAssignment = assignment.make(assignmentBuilder);
mathAssignment.announcement(); 

You had to complete the code implementation of this program. From the first line, we know that we need to have a class Assignment that accepts no parameters. So, let’s declare that first.

Press + to interact
function Assignment() {
}
var assignment = new Assignment();
var assignmentBuilder = new AssignmentBuilder("Math","Hard","12th June, 2020");
var mathAssignment = assignment.make(assignmentBuilder);
mathAssignment.announcement();

The code above gives an error since some other functions/methods being used are not defined. So, ...