-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram-12.js
More file actions
40 lines (32 loc) · 1.31 KB
/
program-12.js
File metadata and controls
40 lines (32 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Write a JavaScript program that creates a class called University with properties for university name and departments. Include methods to add a department, remove a department, and display all departments. Create an instance of the University class and add and remove departments.
class University {
constructor(name) {
this.name = name;
this.departments = [];
}
addDepartment(departmentName) {
this.departments.push(departmentName);
}
removeDepartment(departmentName) {
const index = this.departments.indexOf(departmentName);
if (index !== -1) {
this.departments.splice(index, 1);
}
}
displayDepartments() {
console.log(`University Name: ${this.name}`);
console.log("Departments:");
for (const department of this.departments) {
console.log(department);
}
}
}
const charusatUniversity = new University("Charusat");
charusatUniversity.addDepartment("Information Technology");
charusatUniversity.addDepartment("Civil Engineering");
charusatUniversity.addDepartment("Computer Science and Engineering");
charusatUniversity.addDepartment("Electronics and Communication");
charusatUniversity.displayDepartments();
charusatUniversity.removeDepartment("Electronics and Communication");
console.log("\nAfter removing department:");
charusatUniversity.displayDepartments();