Skip to content

Add solution for Challenge 3 by Falasefemi2#1230

Merged
github-actions[bot] merged 2 commits intoRezaSi:mainfrom
Falasefemi2:challenge-3-Falasefemi2
Apr 14, 2026
Merged

Add solution for Challenge 3 by Falasefemi2#1230
github-actions[bot] merged 2 commits intoRezaSi:mainfrom
Falasefemi2:challenge-3-Falasefemi2

Conversation

@Falasefemi2
Copy link
Copy Markdown
Contributor

Challenge 3 Solution

Submitted by: @Falasefemi2
Challenge: Challenge 3

Description

This PR contains my solution for Challenge 3.

Changes

  • Added solution file to challenge-3/submissions/Falasefemi2/solution-template.go

Testing

  • Solution passes all test cases
  • Code follows Go best practices

Thank you for reviewing my submission! 🚀

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Jan 29, 2026

Walkthrough

A new Go program introduces an Employee struct and Manager type with methods to add, remove, find, and compute average salary for employees. The implementation demonstrates basic employee management operations with a main function that creates and manipulates employee records.

Changes

Cohort / File(s) Summary
New Employee Management Solution
challenge-3/submissions/Falasefemi2/solution-template.go
Adds Employee struct (ID, Name, Age, Salary) and Manager struct with methods: AddEmployee appends employees, RemoveEmployee deletes by ID using slices.DeleteFunc, GetAverageSalary computes average (with integer cast), and FindEmployeeByID returns matching employee pointer. Main function demonstrates full workflow with two employee operations and result printing.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and accurately summarizes the main change: adding a Challenge 3 solution submission by a specific contributor.
Description check ✅ Passed The description is well-structured and directly related to the changeset, providing context about the Challenge 3 submission, file location, and testing confirmation.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@challenge-3/submissions/Falasefemi2/solution-template.go`:
- Around line 39-47: GetAverageSalary in Manager is wrong: it casts Salary to
int losing precision, divides by 2, and doesn't guard against empty Employees.
Fix by accumulating salaries as float64 (e.g., sum := 0.0 and sum += emp.Salary
as float64), return 0.0 if len(m.Employees) == 0 to avoid division by zero, and
compute the average as sum / float64(len(m.Employees)). Ensure you update only
the GetAverageSalary method and refer to Manager, Employees, and Salary.
🧹 Nitpick comments (3)
challenge-3/submissions/Falasefemi2/solution-template.go (3)

19-29: Redundant copy and leftover TODO comment.

The employee variable is unnecessary since e is already passed by value. Also, the // TODO comment should be removed now that the method is implemented.

♻️ Suggested simplification
 // AddEmployee adds a new employee to the manager's list.
 func (m *Manager) AddEmployee(e Employee) {
-	// TODO: Implement this method
-		employee := Employee{
-		ID:     e.ID,
-		Name:   e.Name,
-		Age:    e.Age,
-		Salary: e.Salary,
-	}
-	m.Employees = append(m.Employees, employee)
+	m.Employees = append(m.Employees, e)
 }

31-37: Clean implementation, remove leftover TODO.

Good use of slices.DeleteFunc. Remove the // TODO comment since the method is implemented.

♻️ Remove TODO comment
 // RemoveEmployee removes an employee by ID from the manager's list.
 func (m *Manager) RemoveEmployee(id int) {
-	// TODO: Implement this method
-		m.Employees = slices.DeleteFunc(m.Employees, func(e Employee) bool {
+	m.Employees = slices.DeleteFunc(m.Employees, func(e Employee) bool {
 		return e.ID == id
 	})
 }

49-58: Correct implementation, remove TODO comment.

Good approach using &m.Employees[i] to return a pointer to the actual slice element rather than the loop variable. Remove the // TODO comment.

♻️ Remove TODO comment
 // FindEmployeeByID finds and returns an employee by their ID.
 func (m *Manager) FindEmployeeByID(id int) *Employee {
-	// TODO: Implement this method
-		for i, emp := range m.Employees {
+	for i, emp := range m.Employees {
 		if emp.ID == id {
 			return &m.Employees[i]
 		}
 	}
 	return nil
 }

Comment on lines +39 to +47
// GetAverageSalary calculates the average salary of all employees.
func (m *Manager) GetAverageSalary() float64 {
// TODO: Implement this method
sum := 0
for _, emp := range m.Employees {
sum += int(emp.Salary)
}
return float64(sum) / 2
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical bug: Divides by 2 instead of employee count.

The average salary calculation is incorrect:

  1. Line 46 divides by 2 instead of len(m.Employees) — this will produce wrong results for any count other than 2 employees.
  2. Line 44 casts Salary to int, unnecessarily losing decimal precision.
  3. No guard against division by zero when Employees is empty.
🐛 Proposed fix
 // GetAverageSalary calculates the average salary of all employees.
 func (m *Manager) GetAverageSalary() float64 {
-	// TODO: Implement this method
-		sum := 0
+	if len(m.Employees) == 0 {
+		return 0
+	}
+	sum := 0.0
 	for _, emp := range m.Employees {
-		sum += int(emp.Salary)
+		sum += emp.Salary
 	}
-	return float64(sum) / 2
+	return sum / float64(len(m.Employees))
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// GetAverageSalary calculates the average salary of all employees.
func (m *Manager) GetAverageSalary() float64 {
// TODO: Implement this method
sum := 0
for _, emp := range m.Employees {
sum += int(emp.Salary)
}
return float64(sum) / 2
}
// GetAverageSalary calculates the average salary of all employees.
func (m *Manager) GetAverageSalary() float64 {
if len(m.Employees) == 0 {
return 0
}
sum := 0.0
for _, emp := range m.Employees {
sum += emp.Salary
}
return sum / float64(len(m.Employees))
}
🤖 Prompt for AI Agents
In `@challenge-3/submissions/Falasefemi2/solution-template.go` around lines 39 -
47, GetAverageSalary in Manager is wrong: it casts Salary to int losing
precision, divides by 2, and doesn't guard against empty Employees. Fix by
accumulating salaries as float64 (e.g., sum := 0.0 and sum += emp.Salary as
float64), return 0.0 if len(m.Employees) == 0 to avoid division by zero, and
compute the average as sum / float64(len(m.Employees)). Ensure you update only
the GetAverageSalary method and refer to Manager, Employees, and Salary.

@github-actions github-actions bot merged commit bc3d520 into RezaSi:main Apr 14, 2026
6 checks passed
@github-actions
Copy link
Copy Markdown

🎉 Auto-merged!

This PR was automatically merged after 2 days with all checks passing.

Thank you for your contribution, @Falasefemi2!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants