Knowee
Questions
Features
Study Tools

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"

Question

  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"
...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

To create a Teacher interface in TypeScript, you can follow these steps:

  1. Create a new directory named task_1 and copy the configuration files (package.json, tsconfig.json, webpack.config.js) into this directory.

  2. Open your preferred code editor and create a new TypeScript file in the task_1 directory. You can name it teacher.ts.

  3. In the teacher.ts file, define the Teacher interface. The interface should have the following properties: firstName, lastName, fullTimeEmployee, yearsOfExperience, and location. The firstName and lastName properties should be of type string and should only be modifiable when a Teacher object is first initialized. The fullTimeEmployee property should be of type boolean and should always be defined. The yearsOfExperience property should be of type number and should be optional. The location property should be of type string and should always be defined.

Here is how you can define the Teacher interface:

interface Teacher {
  firstName: string;
  lastName: string;
  fullTimeEmployee: boolean;
  yearsOfExperience?: number;
  location: string;
  [propName: string]: any;
}

The [propName: string]: any; line allows you to add any additional properties to the Teacher object without specifying their names.

  1. Now, you can create a new Teacher object and log it to the console:
const teacher3: Teacher = {
  firstName: 'John',
  fullTimeEmployee: false,
  lastName: 'Doe',
  location: 'London',
  contract: false,
};

console.log(teacher3);

When you run this code, it should print the following output:

Object
contract: false
firstName: "John"
fullTimeEmployee: false
lastName: "Doe"
location: "London"

This problem has been solved

Similar Questions

4. Writing a classmandatoryWrite a Class named StudentClass:The constructor accepts firstName(string) and lastName(string) argumentsThe class has a method named workOnHomework that return the string Currently workingThe class has a method named displayName. It returns the firstName of the studentThe constructor of the class should be described through an InterfaceThe class should be described through an InterfaceRequirements:You can reuse the Webpack configuration from the previous exercise to compile the code.When running npm run build, no TypeScript error should be displayed.Every variable should use TypeScript when possible.

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

2. A Course, Getters, and SettersmandatoryImplement a class named HolbertonCourse:Constructor attributes:name (String)length (Number)students (array of Strings)Make sure to verify the type of attributes during object creationEach attribute must be stored in an “underscore” attribute version (ex: name is stored in _name)Implement a getter and setter for each attribute.bob@dylan:~$ cat 2-main.jsimport HolbertonCourse from "./2-hbtn_course.js";const c1 = new HolbertonCourse("ES6", 1, ["Bob", "Jane"])console.log(c1.name);c1.name = "Python 101";console.log(c1);try { c1.name = 12;} catch(err) { console.log(err);}try { const c2 = new HolbertonCourse("ES6", "1", ["Bob", "Jane"]);}catch(err) { console.log(err);}bob@dylan:~$ bob@dylan:~$ npm run dev 2-main.js ES6HolbertonCourse { _name: 'Python 101', _length: 1, _students: [ 'Bob', 'Jane' ]}TypeError: Name must be a string ...TypeError: Length must be a number ...bob@dylan:~$

A module consists of, exept ?AfunctionsBpackagesCvariablesDclasses

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

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.