The following exercises are designed for developers who already understand JavaScript objects and want to practice solving real-world problems. Each program includes a scenario, input, expected output, and requirements without providing the solution.
1. Find the Employee with the Highest Salary
Scenario
A company stores employee details in an object. Find the employee who has the highest salary.
Input
const employees = {
emp1: { name: "John", salary: 55000 },
emp2: { name: "Alice", salary: 72000 },
emp3: { name: "David", salary: 68000 }
};
Expected Output
Highest Salary Employee:
Alice
Salary: 72000
2. Count Product Categories
Scenario
An online shopping website stores products with categories. Count how many products belong to each category.
Input
const products = {
p1: { category: "Electronics" },
p2: { category: "Furniture" },
p3: { category: "Electronics" },
p4: { category: "Books" },
p5: { category: "Books" }
};
Expected Output
Electronics : 2
Furniture : 1
Books : 2
3. Merge Student Information
Scenario
Merge two student objects into one object.
Input
const personal = {
name: "Rahul",
age: 21
};
const academic = {
course: "B.Tech",
marks: 88
};
Expected Output
{
name: "Rahul",
age: 21,
course: "B.Tech",
marks: 88
}
4. Find Missing Properties
Scenario
Check whether every employee object contains an email property.
Input
const employees = {
emp1: {
name: "John",
email: "john@test.com"
},
emp2: {
name: "Alice"
},
emp3: {
name: "David",
email: "david@test.com"
}
};
Expected Output
Employee Missing Email:
Alice
5. Calculate Total Shopping Cart Value
Scenario
Calculate the total bill of all products in the shopping cart.
Input
const cart = {
item1: {
name: "Mouse",
price: 700,
quantity: 2
},
item2: {
name: "Keyboard",
price: 1200,
quantity: 1
},
item3: {
name: "Monitor",
price: 9500,
quantity: 1
}
};
Expected Output
Total Cart Value:
12100
6. Remove Null Values from Object
Scenario
Remove all properties whose value is null.
Input
const user = {
name: "Deepesh",
phone: null,
city: "Bhopal",
email: null,
age: 30
};
Expected Output
{
name: "Deepesh",
city: "Bhopal",
age: 30
}
7. Find Duplicate Values
Scenario
Identify duplicate values present in an object.
Input
const students = {
s1: "A",
s2: "B",
s3: "A",
s4: "C",
s5: "B"
};
Expected Output
Duplicate Values:
A
B
8. Convert Object into Sorted Array
Scenario
Convert the object values into an array and sort them in ascending order.
Input
const marks = {
maths: 78,
science: 91,
english: 65,
computer: 99
};
Expected Output
[65, 78, 91, 99]
9. Update Nested Object
Scenario
Update the city of the employee.
Input
const employee = {
id: 101,
name: "John",
address: {
city: "Delhi",
state: "Delhi"
}
};
Task
Update city to Mumbai.
Expected Output
{
id:101,
name:"John",
address:{
city:"Mumbai",
state:"Delhi"
}
}
10. Find Average Salary
Scenario
Calculate the average salary of all employees.
Input
const employees = {
emp1: { salary: 45000 },
emp2: { salary: 60000 },
emp3: { salary: 75000 },
emp4: { salary: 50000 }
};
Expected Output
Average Salary:
57500
11. Inventory Stock Checker
Scenario
Display all products whose quantity is less than 5.
Input
const inventory = {
p1: { name: "Laptop", quantity: 3 },
p2: { name: "Keyboard", quantity: 8 },
p3: { name: "Mouse", quantity: 2 },
p4: { name: "Monitor", quantity: 10 }
};
Expected Output
Low Stock Products:
Laptop
Mouse
12. Group Employees by Department
Scenario
Group employees based on department.
Input
const employees = {
emp1: { name: "John", department: "IT" },
emp2: { name: "Alice", department: "HR" },
emp3: { name: "David", department: "IT" },
emp4: { name: "Emma", department: "Finance" }
};
Expected Output
{
IT: ["John", "David"],
HR: ["Alice"],
Finance: ["Emma"]
}
13. Find the Most Expensive Product
Scenario
Find the product with the highest price.
Input
const products = {
p1: { name: "Phone", price: 25000 },
p2: { name: "Laptop", price: 65000 },
p3: { name: "Watch", price: 12000 }
};
Expected Output
Laptop
65000
14. Count Boolean Values
Scenario
Count how many properties have true and false values.
Input
const permissions = {
read: true,
write: false,
delete: true,
update: false,
share: true
};
Expected Output
True : 3
False : 2
15. Reverse Key-Value Pairs
Scenario
Swap the keys and values of an object.
Input
const countryCodes = {
India: "IN",
America: "US",
Japan: "JP"
};
Expected Output
{
IN: "India",
US: "America",
JP: "Japan"
}