Knowee
Questions
Features
Study Tools

5. Advanced types Part 1mandatoryCreate the DirectorInterface interface with the 3 expected methods:workFromHome() returning a stringgetCoffeeBreak() returning a stringworkDirectorTasks() returning a stringCreate the TeacherInterface interface with the 3 expected methods:workFromHome() returning a stringgetCoffeeBreak() returning a stringworkTeacherTasks() returning a stringCreate a class Director that will implement DirectorInterfaceworkFromHome should return the string Working from homegetToWork should return the string Getting a coffee breakworkDirectorTasks should return the string Getting to director tasksCreate a class Teacher that will implement TeacherInterfaceworkFromHome should return the string Cannot work from homegetCoffeeBreak should return the string Cannot have a breakworkTeacherTasks should return the string Getting to workCreate a function createEmployee with the following requirements:It can return either a Director or a Teacher instanceIt accepts 1 arguments:salary(either number or string)if salary is a number and less than 500 - It should return a new Teacher. Otherwise it should return a DirectorExpected result:console.log(createEmployee(200));Teacherconsole.log(createEmployee(1000));Directorconsole.log(createEmployee('$500'));Director

Question

  1. Advanced types Part 1mandatoryCreate the DirectorInterface interface with the 3 expected methods:workFromHome() returning a stringgetCoffeeBreak() returning a stringworkDirectorTasks() returning a stringCreate the TeacherInterface interface with the 3 expected methods:workFromHome() returning a stringgetCoffeeBreak() returning a stringworkTeacherTasks() returning a stringCreate a class Director that will implement DirectorInterfaceworkFromHome should return the string Working from homegetToWork should return the string Getting a coffee breakworkDirectorTasks should return the string Getting to director tasksCreate a class Teacher that will implement TeacherInterfaceworkFromHome should return the string Cannot work from homegetCoffeeBreak should return the string Cannot have a breakworkTeacherTasks should return the string Getting to workCreate a function createEmployee with the following requirements:It can return either a Director or a Teacher instanceIt accepts 1 arguments:salary(either number or string)if salary is a number and less than 500 - It should return a new Teacher. Otherwise it should return a DirectorExpected result:console.log(createEmployee(200));Teacherconsole.log(createEmployee(1000));Directorconsole.log(createEmployee('$500'));Director
...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is the solution in TypeScript:

interface DirectorInterface {
    workFromHome(): string;
    getCoffeeBreak(): string;
    workDirectorTasks(): string;
}

interface TeacherInterface {
    workFromHome(): string;
    getCoffeeBreak(): string;
    workTeacherTasks(): string;
}

class Director implements DirectorInterface {
    workFromHome(): string {
        return 'Working from home';
    }
    getCoffeeBreak(): string {
        return 'Getting a coffee break';
    }
    workDirectorTasks(): string {
        return 'Getting to director tasks';
    }
}

class Teacher implements TeacherInterface {
    workFromHome(): string {
        return 'Cannot work from home';
    }
    getCoffeeBreak(): string {
        return 'Cannot have a break';
    }
    workTeacherTasks(): string {
        return 'Getting to work';
    }
}

function createEmployee(salary: number | string): Director | Teacher {
    if (typeof salary === 'number' && salary < 500) {
        return new Teacher();
    } else {
        return new Director();
    }
}

console.log(createEmployee(200)); // Teacher
console.log(createEmployee(1000)); // Director
console.log(createEmployee('$500')); // Director

This code first defines two interfaces, DirectorInterface and TeacherInterface, each with three methods. Then it defines two classes, Director and Teacher, that implement these interfaces. The createEmployee function takes a salary as an argument and returns an instance of Teacher if the salary is a number less than 500, and an instance of Director otherwise.

This problem has been solved

Similar Questions

2. Extending the Teacher classmandatoryWrite an interface named Directors that extends Teacher. It requires an attribute named numberOfReports(number)Example:const director1: Directors = { firstName: 'John', lastName: 'Doe', location: 'London', fullTimeEmployee: true, numberOfReports: 17,};console.log(director1);// should print// Object// firstName: "John"// fullTimeEmployee: true// lastName: "Doe"// location: "London"// numberOfReports: 17

1. Let's build a Teacher interfacemandatoryCreate a directory task_1 and copy these configuration files into this folder: package.json, tsconfig.json, webpack.config.jsfirstName(string) and lastName(string). These two attributes should only be modifiable when a Teacher is first initializedfullTimeEmployee(boolean) this attribute should always be definedyearsOfExperience(number) this attribute is optionallocation(string) this attribute should always be definedAdd the possibility to add any attribute to the Object like contract(boolean) without specifying the name of the attributeExample:const teacher3: Teacher = { firstName: 'John', fullTimeEmployee: false, lastName: 'Doe', location: 'London', contract: false,};console.log(teacher3);// should print// Object// contract: false// firstName: "John"// fullTimeEmployee: false// lastName: "Doe"// location: "London"

Select the correct answerWhich of the following is used for implementing inheritance through an interface?Optionsinheritedusingextendsimplements

elect the correct answerAmong the following, choose the correct keyword that can be used by a class to use an interface defined previously.Optionsimportextendsimplementsinherits

Select the correct answerWhich of the following is used for implementing inheritance through an interface?Optionsusinginheritedextendsimplements

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.