settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Training Course (Simple Class)

+2 votes

Write a JavaScript class TrainingCourse to hold training course: a course has a title, a trainer and ordered list of topics.

class TrainingCourse {
    // TODO: implement this class
}

Each topic holds title and date. Topics are ordered by date (ascending). Implement the following features:

  • Constructor – creates a training course by title and trainer (strings).
  • Method addTopic(title, date) – adds a topic to the course. The topic consists of topic title (string) and topic date (JavaScript Date object). Topics in the course internally should be ordered by date.
  • Accessor property firstTopic – returns the earliest (by date) topic from the course – a JS object holding title (string) and date (JS Date object).
  • Accessor property lastTopic – returns the latest (by date) topic from the course – a JS object holding title (string) and date (JS Date object).
  • Method toString() – returns a string representing the course and its topics in the following format:
    • Without topics:
      Course "<title>" by <trainer>
      • With topics:
        Course "<title>" by <trainer>
         * <topic 1> - <date 1>
         * <topic 2> - <date 2>
         * …
        
        • Assume there are no topics on the same date.
        • Print all dates in their default format (no additional date / time formatting is required)

Examples:

This is an example how the TrainingCourse class is intended to be used:

let js = new TrainingCourse("JS Intro", "Svetlin Nakov");
console.log("First topic: " + JSON.stringify(js.firstTopic));
console.log("Last topic: " + JSON.stringify(js.lastTopic));
console.log("" + js);

js.addTopic("Maps", new Date(2016, 9, 6, 18, 0));
js.addTopic("JS Overview", new Date(2016, 8, 27, 18, 0));
js.addTopic("Program Logic", new Date(2016, 8, 29, 18, 0));
js.addTopic("Arrays", new Date(2016, 9, 3, 18, 0));
console.log("First topic: " + JSON.stringify(js.firstTopic));
console.log("Last topic: " + JSON.stringify(js.lastTopic));
console.log("" + js);

let php = new TrainingCourse("PHP Intro", "Ivan Yonkov")
    .addTopic("Strings", new Date(2017, 2, 16, 18, 0))
    .addTopic("PHP First Steps", new Date(2017, 2, 12, 18, 0))
    .addTopic("Arrays", new Date(2017, 2, 14, 18, 0));
console.log("" + php);

Corresponding output:

First topic: undefined
Last topic: undefined
Course "JS Intro" by Svetlin Nakov

First topic: {"title":"JS Overview","date":"2016-09-27T15:00:00.000Z"}
Last topic: {"title":"Maps","date":"2016-10-06T15:00:00.000Z"}
Course "JS Intro" by Svetlin Nakov
 * JS Overview - Tue Sep 27 2016 18:00:00 GMT+0300 (FLE Daylight Time)
 * Program Logic - Thu Sep 29 2016 18:00:00 GMT+0300 (FLE Daylight Time)
 * Arrays - Mon Oct 03 2016 18:00:00 GMT+0300 (FLE Daylight Time)
 * Maps - Thu Oct 06 2016 18:00:00 GMT+0300 (FLE Daylight Time)
Course "PHP Intro" by Ivan Yonkov
 * PHP First Steps - Sun Mar 12 2017 18:00:00 GMT+0200 (FLE Standard Time)
 * Arrays - Tue Mar 14 2017 18:00:00 GMT+0200 (FLE Standard Time)
 * Strings - Thu Mar 16 2017 18:00:00 GMT+0200 (FLE Standard Time)

 

asked in JavaScript category by user ak47seo

1 Answer

+1 vote

The solution:

class TrainingCourse {
    constructor(title, trainer) {
        this.title = title;
        this.trainer = trainer;
        this.topicsList = [];
    }

    addTopic(title, date) {
        this.topicsList.push({title, date});
        this.topicsList.sort((t1, t2) => t1.date - t2.date);
        return this;
    }

    get firstTopic() {
        return this.topicsList[0];
    }

    get lastTopic() {
        return this.topicsList[this.topicsList.length - 1];
    }

    toString() {
        let topicsStr = this.topicsList.map(m =>
        ' * ' + m.title + ' - ' + m.date)
            .join("\n");
        return 'Course "' + this.title + '" by ' +
            this.trainer + '\n' + topicsStr;
    }
}

let js = new TrainingCourse("JS Intro", "Svetlin Nakov");
console.log("First topic: " + JSON.stringify(js.firstTopic));
console.log("Last topic: " + JSON.stringify(js.lastTopic));
console.log("" + js);

js.addTopic("Maps", new Date(2016, 9, 6, 18, 0));
js.addTopic("JS Overview", new Date(2016, 8, 27, 18, 0));
js.addTopic("Program Logic", new Date(2016, 8, 29, 18, 0));
js.addTopic("Arrays", new Date(2016, 9, 3, 18, 0));
console.log("First topic: " + JSON.stringify(js.firstTopic));
console.log("Last topic: " + JSON.stringify(js.lastTopic));
console.log("" + js);

let php = new TrainingCourse("PHP Intro", "Ivan Yonkov")
    .addTopic("Strings", new Date(2017, 2, 16, 18, 0))
    .addTopic("PHP First Steps", new Date(2017, 2, 12, 18, 0))
    .addTopic("Arrays", new Date(2017, 2, 14, 18, 0));
console.log("" + php);

 

answered by user eiorgert
...