Add solution for Challenge 3 by Falasefemi2#1230
Add solution for Challenge 3 by Falasefemi2#1230github-actions[bot] merged 2 commits intoRezaSi:mainfrom
Conversation
WalkthroughA 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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ 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. Comment |
There was a problem hiding this comment.
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
employeevariable is unnecessary sinceeis already passed by value. Also, the// TODOcomment 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// TODOcomment 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// TODOcomment.♻️ 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 }
| // 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 | ||
| } |
There was a problem hiding this comment.
Critical bug: Divides by 2 instead of employee count.
The average salary calculation is incorrect:
- Line 46 divides by
2instead oflen(m.Employees)— this will produce wrong results for any count other than 2 employees. - Line 44 casts
Salarytoint, unnecessarily losing decimal precision. - No guard against division by zero when
Employeesis 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.
| // 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.
|
🎉 Auto-merged! This PR was automatically merged after 2 days with all checks passing. Thank you for your contribution, @Falasefemi2! |
Challenge 3 Solution
Submitted by: @Falasefemi2
Challenge: Challenge 3
Description
This PR contains my solution for Challenge 3.
Changes
challenge-3/submissions/Falasefemi2/solution-template.goTesting
Thank you for reviewing my submission! 🚀