nested_object.html
Sun Nov 03 2024 16:29:47 GMT+0000 (Coordinated Universal Time)
Saved by @signup1
<!--DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Company Information</title>
</head>
<body>
<h1>Company Information</h1>
<pre id="output"></pre>
<script>
// Define a nested object for a company
const company = {
name: "GeeksforGeeks",
location: "Noida",
employees: {
count: 100,
departments: ["Development", "Design", "Marketing"],
details: [
{ name: "John Doe", position: "Developer" },
{ name: "Jane Smith", position: "Designer" }
]
}
};
// Function to display company information
function displayCompanyInfo(company) {
let output = "";
output += "Company Name: " + company.name + "\n";
output += "Location: " + company.location + "\n";
output += "Employee Count: " + company.employees.count + "\n";
output += "Departments: " + company.employees.departments.join(", ") + "\n\n";
output += "Employee Details:\n";
company.employees.details.forEach(employee => {
output += `- ${employee.name}, Position: ${employee.position}\n`;
});
return output;
}
// Function to add a new employee
function addEmployee(company, employeeName, employeePosition) {
const newEmployee = { name: employeeName, position: employeePosition };
company.employees.details.push(newEmployee);
company.employees.count++;
}
// Function to update the department list
function addDepartment(company, departmentName) {
if (!company.employees.departments.includes(departmentName)) {
company.employees.departments.push(departmentName);
return `Department "${departmentName}" added.`;
} else {
return `Department "${departmentName}" already exists.`;
}
}
// Display original company info
let result = "Original Company Information:\n";
result += displayCompanyInfo(company);
// Add a new employee
addEmployee(company, "Alice Johnson", "Project Manager");
result += "\nAfter Adding New Employee:\n";
result += displayCompanyInfo(company);
// Add a new department
result += addDepartment(company, "Sales") + "\n";
result += "\nAfter Adding New Department:\n";
result += displayCompanyInfo(company);
// Attempt to add an existing department
result += addDepartment(company, "Development") + "\n";
// Display the result in the HTML page
document.getElementById("output").textContent = result;
</script>
</body>
</html-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Company Info</title>
</head>
<body>
<h1>Company Information</h1>
<pre id="output"></pre>
<script>
// Define a simple nested object for a company
const company = {
name: "GeeksforGeeks",
location: "Noida",
employees: {
count: 100,
departments: ["Development", "Design", "Marketing"]
}
};
// Function to display company information
function displayCompanyInfo(company) {
return `Company Name: ${company.name}\nLocation: ${company.location}\nEmployee Count: ${company.employees.count}\nDepartments: ${company.employees.departments.join(", ")}`;
}
// Display the company information in the HTML page
document.getElementById("output").textContent = displayCompanyInfo(company);
</script>
</body>
</html>



Comments