diff --git a/javascript-algorithms-and-data-structures/workshop-smart-campus-directory/data.json b/javascript-algorithms-and-data-structures/workshop-smart-campus-directory/data.json new file mode 100644 index 000000000..405078c59 --- /dev/null +++ b/javascript-algorithms-and-data-structures/workshop-smart-campus-directory/data.json @@ -0,0 +1,203 @@ +{ + "Computer Science and Engineering": { + "Dept_code": "CSE", + "programs": [ + { + "id": "CSE101", + "name": "Database Management System", + "buildingCode": "CSE", + "room": "402", + "instructors": [ + { + "id": "INS001", + "name": "Alejandro", + "email": "alejandro@edu.com", + "officeHours": "Sun-Tue 10AM-12PM" + }, + { + "id": "INS002", + "name": "Kenji", + "email": "kenji@edu.com", + "officeHours": null + } + ] + }, + { + "id": "CSE102", + "name": "Data Structures and Algorithms", + "buildingCode": "CSE", + "room": "205", + "instructors": [ + { + "id": "INS003", + "name": "Fatima", + "email": "fatima@edu.com", + "officeHours": "Mon-Wed 2PM-4PM" + }, + { + "id": "INS004", + "name": "Mei", + "email": "mei@edu.com" + } + ] + }, + { + "id": "CSE103", + "name": "Operating Systems", + "buildingCode": "CSE", + "room": null, + "instructors": [ + { + "id": "INS005", + "name": "Dmitri", + "email": "dmitri@edu.com", + "officeHours": "Thu 11AM-1PM" + } + ] + }, + { + "id": "CSE104", + "name": "Computer Networks", + "buildingCode": "CSE", + "room": "510", + "instructors": [] + } + ] + }, + "Electrical and Electronic Engineering": { + "Dept_code": "EEE", + "programs": [ + { + "id": "EEE101", + "name": "Digital Logic Design", + "buildingCode": "EEE", + "room": "101", + "instructors": [ + { + "id": "INS006", + "name": "Javier", + "email": "javier@edu.com", + "officeHours": "Sun 9AM-11AM" + } + ] + }, + { + "id": "EEE102", + "name": "Electronic Devices and Circuits", + "buildingCode": "EEE", + "room": "303", + "instructors": [ + { + "id": "INS007", + "name": "Isabella", + "email": "isabella@edu.com", + "officeHours": null + }, + { + "id": "INS008", + "name": "Omar", + "email": null, + "officeHours": "Tue 3PM-5PM" + } + ] + }, + { + "id": "EEE103", + "name": "Power Systems", + "room": "404", + "instructors": [ + { + "id": "INS009", + "name": "Yuki", + "email": "yuki@edu.com", + "officeHours": "Mon 10AM-12PM" + } + ] + } + ] + }, + "Business Administration": { + "Dept_code": "BBA", + "programs": [ + { + "id": "BBA101", + "name": "Financial Accounting", + "buildingCode": "BBA", + "room": "201", + "instructors": [ + { + "id": "INS010", + "name": "Sofia", + "email": "sofia@edu.com", + "officeHours": "Wed 1PM-3PM" + } + ] + }, + { + "id": "BBA102", + "name": "Marketing Principles", + "buildingCode": "BBA", + "room": "305", + "instructors": [ + { + "id": "INS011", + "name": "Mateo", + "email": "mateo@edu.com" + } + ] + }, + { + "id": "BBA103", + "name": "Business Analytics", + "buildingCode": "BBA", + "room": null, + "instructors": [] + } + ] + }, + "Architecture": { + "Dept_code": "ARC", + "programs": [ + { + "id": "ARC101", + "name": "Urban Design", + "buildingCode": "ARC", + "room": "601", + "instructors": [ + { + "id": "INS012", + "name": "Elena", + "email": "elena@edu.com", + "officeHours": "Thu 2PM-4PM" + } + ] + }, + { + "id": "ARC102", + "name": "Architectural Drawing", + "buildingCode": "ARC", + "room": "603", + "instructors": [ + { + "id": "INS013", + "name": "Santiago", + "email": "santiago@edu.com", + "officeHours": null + } + ] + }, + { + "id": "ARC103", + "name": "Landscape Planning", + "buildingCode": "ARC", + "instructors": [ + { + "id": "INS014", + "name": "Aisha", + "email": "aisha@edu.com" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/javascript-algorithms-and-data-structures/workshop-smart-campus-directory/script.js b/javascript-algorithms-and-data-structures/workshop-smart-campus-directory/script.js new file mode 100644 index 000000000..29cf22943 --- /dev/null +++ b/javascript-algorithms-and-data-structures/workshop-smart-campus-directory/script.js @@ -0,0 +1,168 @@ +//this data will be fetched from CDN +const fs = require("fs"); +const data = fs.readFileSync("data.json", "utf8"); +const rawData = JSON.parse(data); +// console.log(rawData); + +function normalizeDirectory(rawData) { + // Lookup tables + const departmentsById = {}; + const programsById = {}; + const instructorsById = {}; + const instructorsByEmail = {}; + + // Convert object into iterable array + const departmentEntries = Object.entries(rawData); + + departmentEntries.forEach(([departmentName, departmentData], index) => { + // Generate department ID + const departmentId = departmentData.Dept_code || `dept-${index + 1}`; + + // Store department + departmentsById[departmentId] = { + id: departmentId, + name: departmentName, + programIds: [], + }; + + // Loop through programs + departmentData.programs.forEach((program) => { + programsById[program.id] = { + ...program, + departmentId, + }; + + departmentsById[departmentId].programIds.push(program.id); + + // Normalize instructors + program.instructors.forEach((instructor) => { + instructorsById[instructor.id] = { + ...instructor, + programId: program.id, + departmentId, + }; + + // Create fast email lookup + if (instructor.email) { + instructorsByEmail[instructor.email] = instructor.id; + } + }); + }); + }); + + return { + departmentsById, + programsById, + instructorsById, + instructorsByEmail, + }; +} + +const normalizedData = normalizeDirectory(rawData); +// console.log(normalizedData); + + + +function getInstructorByEmail(email, normalizedData) { + + const instructorId = + normalizedData.instructorsByEmail[email]; + + if (!instructorId) { + return "Instructor not found"; + } + + const instructor = + normalizedData.instructorsById[instructorId]; + + const department = + normalizedData.departmentsById[ + instructor?.departmentId + ]; + + return { + name: instructor?.name ?? "Unknown Instructor", + + officeHours: + instructor?.officeHours ?? + "Not Available", + + department: + department?.name ?? + "Unknown Department" + }; +} + +// console.log( +// getInstructorByEmail( +// "dmitri@edu.com", +// normalizedData +// ) +// ); + + +function listRoomsByBuilding(buildingCode, normalizedData) { + + return Object.values(normalizedData.programsById) + + .filter(program => + program.buildingCode === buildingCode + ) + + .map(program => program.room) + + .filter(room => room != null) + + .sort((a, b) => a.localeCompare(b, undefined, { + numeric: true + })); +} + +// List rooms +// console.log( +// listRoomsByBuilding( +// "CSE", +// normalizedData +// ) +// ); + + +function renderDirectorySummary(normalizedData) { + + const departments = + Object.keys(normalizedData.departmentsById).length; + + const programs = + Object.keys(normalizedData.programsById).length; + + const instructors = + Object.keys(normalizedData.instructorsById).length; + + const missingOfficeHours = + Object.values(normalizedData.instructorsById) + .filter(instructor => + instructor.officeHours == null + ).length; + + const programsWithoutRooms = + Object.values(normalizedData.programsById) + .filter(program => + program.room == null + ).length; + + return ` +Campus Directory Summary +------------------------ +Departments: ${departments} +Programs: ${programs} +Instructors: ${instructors} +Missing Office Hours: ${missingOfficeHours} +Programs Without Rooms: ${programsWithoutRooms} +`; +} + +console.log( + renderDirectorySummary( + normalizedData + ) +); \ No newline at end of file